更新4:
这是我目前拥有的:
var $elements = $('.dropdown');
var $lastElement = $elements.last();
$elements.slice(0, -1).each(function() {
$(this).css('left', $(this).prev.position().left);
});
var $prev = $lastElement.prev();
$lastElement.css('left', $prev.position().left - ($prev.outerWidth() - $lastElement.outerWidth()));
这给了我:
Uncaught TypeError: Object function (d,e){var f=c.map(this,b,d);Za.test(a)||(e=d);if(e&&typeof e==="string")f=c.filter(e,f);f=this.length>1?c.unique(f):f;if((this.length>1||ab.test(e))&&$a.test(a))f=f.reverse();return this.pushStack(f,a,bb.call(arguments).join(","))} has no method 'position'
$.ajax.success homepage.htm:138
c.b.extend.each jquery.min.js:33
c.b.fn.b.each jquery.min.js:26
$.ajax.success homepage.htm:137
c.extend.handleSuccess jquery.min.js:142
c.extend.ajax.L.w.onreadystatechange jquery.min.js:141
因此,上面的脚本中没有位置方法。
更新3:
这是我目前拥有的:
变量$elements = $('.dropdown');变量$lastElement = $elements.last();
$elements.slice(0,-1).each(函数(){$(This).css(‘左’,$(this).prev.position().left);});
var $prev = $lastElement.prev();$lastElement.css(‘左’,$prev.position().left - ($prev.outerWidth() -$lastElement.outerWidth();
这给了我一个function required
错误。当我去掉最后两行时,错误就消失了,但是问题没有得到解决。
更新2:
这是HTML结构:
<div class="settings"><span id="spanSettings">Settings ▼</span></div>
<div class="dropdown">
<a href="edit_profile.aspx"><div class="item"><div class="icon" style="background-image: url('http://intranet/img/profile_icon.png');"></div> Edit profile</div></a>
<a href="manage_tabs.aspx"><div class="item"><div class="icon" style="background-image: url('http://intranet/img/tabs_icon.png');"></div> Tab settings</div></a>
</div>
更新1:
与:$(this).css('left', $(this).prev().position().left);
我得到了图1
与:$(this).css('right', $(this).prev().position().right);
我得到了图2:
我在追图3
^图像3是光刻的。
原始问题:
我有下面的脚本,它改变了具有类dropdown
的html元素的位置
$('.dropdown').each(function() {
$(this).css('left', $(this).prev().position().left);
});
我如何修改它,以便根据父级将所有的dropdown
更改为左,除了最后一个应该与父级右位置对齐的right
?
如果这是不可能的,我如何独立地改变自己的立场?而不是使用each
函数?
发布于 2011-07-07 15:04:25
你可以这样做:
var $elements = $('.dropdown'),
$lastElement = $elements.last();
$elements.slice(0, -1).each(function() {
$(this).css('left', $(this).prev().position().left);
});
$lastElement.css('right', $lastElement.prev().position().right);
Update:,如您在图片中显示的那样对齐它(我假设它是最后一个元素),您可以尝试:
var $prev = $lastElement.prev();
$lastElement.css('left', $prev.position().left - ($prev.outerWidth() - $lastElement.outerWidth()));
发布于 2011-07-07 15:15:47
您可以为此编写一个快速插件:
$.fn.eachLast = function(fn, fnLast) {
return this.slice(0, -1).each(fn).end().last().each(fnLast).end();
}
$('.dropdown').eachLast(
function() { // will be executed for all elements in the collection except the last one
var $this = $(this);
$this.css('left', $this.parent().position().left);
},
function() { // will only be executed for the last element in the collection
var $this = $(this);
$this.css('right', $this.prev().position().right);
}
);
或者,您也可以这样做:
var $lastElement = $('.dropdown').slice(0, -1).each(function() {
var $this = $(this);
$this.css('left', $this.prev().position().left);
}).end().last();
$lastElement.css('right', $lastElement.parent().position().right);
// …or even…
var $lastElement = $('.dropdown').slice(0, -1).each(function() {
var $this = $(this);
$this.css($this.parent().position());
}).end().last();
$lastElement.css($lastElement.prev().position());
https://stackoverflow.com/questions/6612701
复制相似问题