开发者

jquery obj.find problem in javascript

开发者 https://www.devze.com 2023-03-19 10:06 出处:网络
I have this snippet of code, which fails at var link: var obj = $(this[0]); var html = obj.html(); var link = html.find(\'a[href*=/comment/reply]\');

I have this snippet of code, which fails at var link:

var obj = $(this[0]);     
var html = obj.html();
var link = html.find('a[href*=/comment/reply]');

This is an ajax response from a submitted form. The output of what I get back from the var html is as follows:

===><div class="comment-new-success"><a id="new"></a>
<a id="comment-482"></a>
<div class="comment">
<div class="submitted">Submitted by <a href="/user/1" title="View user profile.">NAME</a> on Sun, 07/10/2011 - 12:48.<span class="new"> *new</span></div>
<div class="content clearfix"><p>123123123123122</p>
</div>
<div class="links_box"><ul class="links"><li class="comment_delete first"><a href="/comment/delete/482?token=e1fba5ef1c99c5a3760313b53f582972">delete</a></li>
<li class="comment_edit"><a href="/comment/edit/482">edit</a></li>
<li class="comment_reply last"><a href="/comment/reply/6/482">reply</a></li>
</ul></div></div>
</div><===(string)

How do I properly get the "/comment/reply/6/482" variable as the var link in the above example. The code (I thought) should work fin开发者_高级运维e, but doesn't


$('a[href*="/comment/reply"]')

See my jsfiddle: http://jsfiddle.net/hCbt3/ I grabbed the text from the link just to demonstrate it's correctly selecting the anchor.

Or grab the more specific one if you have more than one comment/reply:

$('a[href*="/comment/reply/6/482"]')


Try this :

$('a[href^="/comment/reply/"]')

Per JQuery docs, the Starts With Selector has the syntax: [name^="value"] and will select elements that have the specified attribute with a value beginning exactly with a given string.


The answer was this:

var link = $(this[0]).find('.comment_reply a').attr('href');
0

精彩评论

暂无评论...
验证码 换一张
取 消