考慮這個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屬性訪問這些文本的正確方法是什麼?
請做出更好的努力,正確地格式化你的代碼。使用一致且正確的縮進使您的代碼更具可讀性。 –