jquery遍历和表
这个问题让我有点抓狂。
我有这样的东西,
<h3>Feeds
<span><a class="smallbutton create_feed" href="javascript:;">
<strong>Create New</strong>
</a>
</span>
<span class="div_form_add_feed">
<a class="smallbutton btn_save_feeds" href="javascript:;">
<strong> Add </strong></a>
</span>
</h3>
<table cellpadding=0 cellspacing=1 class="table">
<tr><td>
<div class="get_feeds_news">test</div></td></tr></table>
我想要添加链接类“smallbutton btn_save_feeds”以用其他内容替换div“get_feeds_news”。但我似乎无法在 jQuery 中使用 $(this) 遍历它。
从添加链接类“smallbutton btn_save_feeds”遍历以更改 div“get_feed_news”中的某些内容的正确方法是什么?我尝试了以下方法,但没有成功。
$(this).closest('.get_feeds_news').html('hihi');
问题是因为它在表格内吗?
提前致谢。
This problem is making me a bit crazy.
I have something like this
<h3>Feeds
<span><a class="smallbutton create_feed" href="javascript:;">
<strong>Create New</strong>
</a>
</span>
<span class="div_form_add_feed">
<a class="smallbutton btn_save_feeds" href="javascript:;">
<strong> Add </strong></a>
</span>
</h3>
<table cellpadding=0 cellspacing=1 class="table">
<tr><td>
<div class="get_feeds_news">test</div></td></tr></table>
I want the Add link class "smallbutton btn_save_feeds" to replace the div "get_feeds_news" with something else. But I cannot seem to traverse to it using $(this) in jQuery.
What is the right way to traverse from the Add link class "smallbutton btn_save_feeds" to change someting in the div "get_feed_news"? I tried the following but didnt work.
$(this).closest('.get_feeds_news').html('hihi');
Is the problem because it's inside a table?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您缺少点(加上结束 div 标签)
you're missing the dot (plus a closing div tag)
closest()
将不起作用,因为您正在搜索的元素不是按钮的祖先。如果
get_feeds_news
是该类的唯一出现,您可以简单地使用$('.get_feeds_news')
否则,您可以执行以下操作:
$(this) .closest('h3').next().find('.get_feeds_news')
jQuery 有多种遍历方式。这当然不是唯一的方法。如果您的布局发生变化,将会有某种方法来完成您的需要。
closest()
will not work since the element you're searching for is not an ancestor of the button.If
get_feeds_news
is the only occurrence of that class, you could simply use$('.get_feeds_news')
Otherwise, you could do something like:
$(this).closest('h3').next().find('.get_feeds_news')
jQuery has many ways to traverse. This is certainly not the only way to go about it. If your layout changes, there will be some way to accomplish what you need.
尝试修复您的 html,您没有关闭 get_feeds_news div:
try fixing your html, you didn't close your get_feeds_news div:
您可以选择两个元素之一
(或者第二个为 1)
,或者,如果您有 2 个链接
smallbutton btn_save_feeds
(每个.get_feed_news
一个),您可以使用:基本上将索引与调用者的索引一起传递给过滤器
.get_feed_news
:]you can select one of two elements by
(or 1 for second)
or, if you have 2 links
smallbutton btn_save_feeds
(one for each.get_feed_news
), you can use:what basically passes index to filter
.get_feed_news
with index of caller :]