作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个主文档 div,通过变量 "str"
添加一些 html 元素
<div id='documentDiv'></div>
我有一个功能,可以将一些 html 元素添加到我的 #documentDiv
。
var str="<div class='customElement'></div><a href='' onclick='append(event);'>append</a>"
而且我有一个功能,可以根据需要多次将 str
附加到 documentDiv
:
$('#documentDiv').append(str);
添加两次后,我在 documentDiv 元素中添加了两次相同的结构
<div id='documentDiv'>
<div class='customElement'></div><a href='' onclick='append(event);'>append</a> // want to append some text to respective .customElement div
<div class='customElement'></div><a href='' onclick='append(event);'>append</a>
</div>
我的问题是 onclick ,我想在 customElement
类下的 respective div 中添加一些东西。但是对于 onClick , 它总是附加到第一个 div。我如何使用 jquery 获取相应的 div 元素?
function append(event){
$('.customElement').append("some text");
}
最佳答案
也许这可以帮助你:没有标签:
$('.customElement').bind('click',function(){
$(this).append("some text");
});
或使用 a 标签:
$('.customElement a').bind('click',function(){
$(this).parent().append("some text");
});
关于javascript - 使用jquery获取同一类下的相应div元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34770458/
我在 Java 中遇到异常处理问题,这是我的代码。当我尝试运行此行时出现编译器错误:throw new MojException("Bledne dane");。错误是: exception MojE
我刚刚开始学习asp.net。在你们的支持下,我希望我能从这个论坛学到更多东西。 我的问题是, 我在 asp.net 页面中有一个 TabContainer1,因为每个选项卡面板中有多个类似 (60)
我是一名优秀的程序员,十分优秀!