jQuery nextUntil 奇怪
我错过了什么或者这个方法是fubar吗?我无法让脚本正确选择,所以我删除了除 jQuery 1.5 之外的所有内容,并尝试将此作为测试:
HTML:
<a href="test.php" id="test" class="notActive">test active</a>
<p><a href="ui.php#content" class="ajax" data-target="loadid">Link 1</a></p>
<p><a href="widgets.html" class="ajax" data-target="loadid">Link 2</a></p>
<div id="loadid" class="right bgGreen pad">test;</div>
jQuery:
var z = $('#test').nextUntil('div');
z.css({'background-color' : '#000000' });
console.log(z.html());
Returns:
<a href="test.php" id="test" class="notActive">test active</a>
<p style="background-color: #000000">
<a href="ui.php#content" class="ajax" data-target="loadid">
</p>
<p style="background-color: #000000">
<a href="widgets.html" class="ajax" data-target="loadid">
</p>
<div id="loadid" class="right bgGreen pad">
LOG: <\a href="ui.php#content" class="ajax" data-target="loadid">Link 1</a>
Also,将选择器从 'div' 更改为 'p' 结果为 null...有什么想法吗?
Am I missing something or is this method fubar? I couldn't get a script to select properly, so I stripped everything out except jQuery 1.5 and tried this as a test:
HTML:
<a href="test.php" id="test" class="notActive">test active</a>
<p><a href="ui.php#content" class="ajax" data-target="loadid">Link 1</a></p>
<p><a href="widgets.html" class="ajax" data-target="loadid">Link 2</a></p>
<div id="loadid" class="right bgGreen pad">test;</div>
jQuery:
var z = $('#test').nextUntil('div');
z.css({'background-color' : '#000000' });
console.log(z.html());
Returns:
<a href="test.php" id="test" class="notActive">test active</a>
<p style="background-color: #000000">
<a href="ui.php#content" class="ajax" data-target="loadid">
</p>
<p style="background-color: #000000">
<a href="widgets.html" class="ajax" data-target="loadid">
</p>
<div id="loadid" class="right bgGreen pad">
LOG: <\a href="ui.php#content" class="ajax" data-target="loadid">Link 1</a>
Also, changing the selector from 'div' to 'p' results null... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你错过了一些东西。来自 jQuery API 文档:
nextUntil()
返回的值包含多个元素,即两个。记录
.html()
返回的值会产生误导,因为.html( )
仅返回集合中第一个对象的内部 HTML。只需记录 jQuery 对象本身,您就应该能够看到其中的内容(至少在 Firebug 中;我不认为 WebKit 的调试器能很好地记录 jQuery 对象):您要选择哪个元素?
You're missing something. From the jQuery API docs:
The value returned by your
nextUntil()
contains multiple elements, namely the two<p>
s . Logging the value returned by.html()
is misleading because.html()
returns only the inner HTML of the first object in the set. Just log the jQuery object itself and you should be able to see what's inside it (at least in Firebug; I don't think WebKit's debugger logs jQuery objects as nicely):Which element are you trying to select?
嗯,这似乎是对的。根据 http://api.jquery.com/nextUntil/ .nextUntil 选择所有同级,直到但不包括参数选择的元素。因此,如果您执行 .nextUntil('div') 您会选择所有段落(它们是 #test 元素的同级)。如果执行 .nextUntil('p') 则不会选择任何内容,因为在 #test 之后会出现一个段落。
Well this seems right. According to http://api.jquery.com/nextUntil/ the .nextUntil selects all the siblings until but not including the element selected by the argument. So if you do .nextUntil('div') you select all the paragraphs (which are siblings to your #test element). If you do .nextUntil('p') nothing gets selected, because a paragraph apprears right after the #test.
我认为你的 .css 语法是错误的。
应该有一个“,”而不是“:”
Your .css syntax is wrong I think.
Should have a "," instead of ":"