Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每个函数的jQuery帮助_Javascript_Jquery - Fatal编程技术网

Javascript 每个函数的jQuery帮助

Javascript 每个函数的jQuery帮助,javascript,jquery,Javascript,Jquery,更新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

更新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
因此,上面脚本中的某些内容没有position方法


更新3:

这就是我目前的情况:

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()));
变量$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())

它给了我一个所需的
函数
错误。当我取出最后两行时,错误消失了,但问题没有得到解决


更新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>
我如何修改它,使其基于父项将所有的
下拉列表
更改为左侧,但最后一个除外,它应该与父项的右侧位置对齐
右侧

如果不可能,我如何独立改变他们的立场?您可以执行以下操作,而不是使用
每个
功能:

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);
更新:要按照图片所示对齐它(我假设它是关于最后一个元素),您可以尝试:

var $prev = $lastElement.prev();
$lastElement.css('left', $prev.position().left - ($prev.outerWidth() - $lastElement.outerWidth()));
你可以做:

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);
更新:要按照图片所示对齐它(我假设它是关于最后一个元素),您可以尝试:

var $prev = $lastElement.prev();
$lastElement.css('left', $prev.position().left - ($prev.outerWidth() - $lastElement.outerWidth()));

您可以为此编写一个快速插件:

$.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());

您可以为此编写一个快速插件:

$.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());

出于某种原因,这给了我一个挤压的下拉列表。我想也许我没有正确地解释我想要什么。基本上,所有下拉列表的左侧应该与其父下拉列表的左侧对齐。然而,在最后一个下拉列表中,如果它是父项,则它的右侧需要与右侧对齐。@oshirowanen:噢,父项。。。然后使用
parent
而不是
prev
。。。更新
prev
为您提供了上一个兄弟姐妹…听起来我应该使用prev。请参阅原始问题中的更新1,因为我认为我没有正确解释问题。我上传了一些图片,让你明白我的意思。@oshirowanen:我想你还需要发布HTML结构。如果你能提供一个演示就更好了。我添加了更新2,它显示了有问题的结构的HTML。出于某种原因,这给了我一个压缩的下拉列表。我想也许我没有正确地解释我想要什么。基本上,所有下拉列表的左侧应该与其父下拉列表的左侧对齐。然而,在最后一个下拉列表中,如果它是父项,则它的右侧需要与右侧对齐。@oshirowanen:噢,父项。。。然后使用
parent
而不是
prev
。。。更新
prev
为您提供了上一个兄弟姐妹…听起来我应该使用prev。请参阅原始问题中的更新1,因为我认为我没有正确解释问题。我上传了一些图片,让你明白我的意思。@oshirowanen:我想你还需要发布HTML结构。如果你能提供一个演示就更好了。我添加了更新2,它显示了问题结构的HTML。请参阅原始问题中的更新1,因为我认为我没有正确解释问题。我上传了一些图片来告诉你我的意思。请看原始问题中的更新1,因为我认为我没有正确解释这个问题。我上传了一些图片,让你明白我的意思。