<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
function foo() {
$(this).parent('li').append('hello world');
}
</script>
<ul>
<li><img src="http://www.gravatar.com/avatar/a3981906ca415e0ec3e051076b71b0cd?s=32&d=identicon&r=PG" onClick="foo()" /></li>
<li><img src="http://www.gravatar.com/avatar/8ec1383b240b5ba15ffb9743fceb3c0e?s=32&d=identicon&r=PG" onClick="foo()" /></li>
</ul>
我正在尝试将字符串“hello world” append 到 <li>
其中包含刚刚单击的图像。
最佳答案
无需 append onclick。使用 jQuery 就可以了
$('li').click(function(){
$(this).append('hello world')
});
检查工作示例 http://jsfiddle.net/tFr5y/
关于javascript - 为什么这个简单的 jQuery 片段不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5062894/