JS/JQuery: I'm trying to move an li node under the one it matches
$(document).ready(function() {
var a = $("li.comment").attr("id");
var b = $("li.comment[id='"+a+"']");
var c = $("li.comment > div.comment-body div > p > a").attr("href");
var d = $("li.comment > div.comment-body div > p > a[href='"+c+"']");
var e = new RegExp("[#+'+b+']b")
if (d===e) {
d.parent().append(a);};
};)
If d
matches b
, I'm trying to move d
(starting from the li
node) to right under b
who's id it matches. I can't figure it out.
Sample:
<li id="wow" class="comment">
<div class="comment-body">
<div>
<p>Any comments?</p>
</div>
</div>
</li>
<li id="rebble" class="comment"><div class="comment-body"><div><p>Wakatow</p></div></div></li>
<li class="comment" id="det">
<div class="comment-body">
<div>
<p>
<a href="#wow">Yeah! Hold on a sec...</a>
</p>
</div>
</div>
</li>
I'm trying to work it out on jsfiddle, but I can't get it done. When it comes out right, the li
with the link is su开发者_运维问答pposed to move under the first one
Can you compare strings?
$(document).ready(function() {
var a = $("li.comment").attr("id");
var b = $("li.comment[id='"+a+"']");
var c = $("li.comment > div.comment-body div > p > a").attr("href");
if( '#'+a == c ){
var d = $("li.comment > div.comment-body div > p > a[href='"+c+"']");
d.parent().append(a);
}
});
http://jsfiddle.net/FjH79/
You cannot compare selectors and string.
What you could do is:
var e = b.attr('id');
if ("#" + d.attr('id')===e) {
d.parents().insertAfter(a);
};
精彩评论