我有这个HTML代码:
<div id="ht_key" class="ht_all"> <div class="ht_bottom"> <div class="ht_bottom_p"> <span class="ht_bottom_o"> <a href="javascript:;" onclick="htrc('key');\">click</a> </span> </div> </div> </div>此代码可在第5-6页(或更多)中看到。 我想从htrc函数中用其他一些内容替换#ht_key div。 (“关键”在每种情况下都不同)。 问题是如果在页面中有2个具有相同“键”的div,那么当htrc函数发生时,则只替换第一个#ht_key div。 所以,因为我需要替换#ht_key div的内容,其中单击a(从中调用htrc函数),是否有任何方法可以替换其父div(具有id的父div)?
我已经尝试了父和最近,但没有一个返回结果。 你有什么建议吗?
谢谢!
i have this html code:
<div id="ht_key" class="ht_all"> <div class="ht_bottom"> <div class="ht_bottom_p"> <span class="ht_bottom_o"> <a href="javascript:;" onclick="htrc('key');\">click</a> </span> </div> </div> </div>This code may be seen in the page 5-6 times(or more). I want from the htrc function to replace the #ht_key div with some other content. ("key" is different in each case). The problem is that if in the page there are 2 divs with same "key" then when the htrc function takes place, then only the first #ht_key div is replaced. So since i need to replace the content of the #ht_key div where the a (from which the htrc function is called) was clicked, is there any way to replace its parent div(the parent div which has id)?
I have tried both parent and closest but none of these returned the result. Do you have any suggestion?
Thanks!
最满意答案
我的建议是让ht_key成为一个class而不是一个id 。
<div class="ht_key ht_all">这样您的HTML就有效了。
或者更改onclick以传递this ,并执行closest() ht_all的closest() :
<a href="#" onclick="htrc(this);">click</a>JS
htrc( el ) { $(el).closest('.ht_all'); // ... }如果采用这种方法,我会为ht_key使用HTML5 data-属性:
<div data-key="somekey" class="ht_all">如果你使用的是最新版本的jQuery,你可以:
htrc( el ) { var $all = $(el).closest('.ht_all'); var key = $all.data('key'); }My suggestion would be to make the ht_key a class instead of an id.
<div class="ht_key ht_all">This way your HTML is valid.
Or change the onclick to pass this, and do a closest() up to ht_all:
<a href="#" onclick="htrc(this);">click</a>js
htrc( el ) { $(el).closest('.ht_all'); // ... }If you take this approach, the I'd use a HTML5 data- attribute for the ht_key:
<div data-key="somekey" class="ht_all">And if you're using the latest versions of jQuery, you can do:
htrc( el ) { var $all = $(el).closest('.ht_all'); var key = $all.data('key'); }更多推荐
发布评论