考虑这个HTML代码:如何使用event.target访问特定的孩子及其文本?
<div id="container">
<div class="c1">Text1
<p class="pc1">Paragraph1</p>
</div>
<div class="c1">Text2
<p class="pc1">Paragraph2</p>
</div>
</div>
而这里的脚本我想:
<script>
$(document).ready(function(){
$("#container").click(function(e){
var a=e.target.children().text();
//I want to store the text in the paragraph (e.g. Paragraph 1, when I click the first div) in var a.
var b=e.target.childNodes[0].nodeValue;
//I want to store the text in the div (e.g. Text1, when I click the first div) in var b.
});
});
</script>
但它不工作。我知道我写错了什么。使用event.target属性访问这些文本的正确方法是什么?
请做出更好的努力,正确地格式化你的代码。使用一致且正确的缩进使您的代码更具可读性。 –