HTML:
<div class="col answer">
some text
</div>
js:
$(".answer").text("Here we change the text" );
$(".answer").click(function(E) {
console.log(E).html();
});
控制台输出:未定义
答案 0 :(得分:0)
您需要使用$(this).html()
而不是(E).html()
。您的代码应如下所示:
console.log($(this).html());
演示:
$(".answer").text("Here we change the text");
$(".answer").click(function() {
console.log($(this).html());
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div class="col answer">
some text
</div>
答案 1 :(得分:0)
由于这也是在Javascript中发布的,因此可以采用相同的香草选项:
<div class="col answer">
some text
</div>
<script>
var x = document.getElementsByClassName("answer");
var y;
for (y = 0; y < x.length; y++) {
x[y].innerHTML = "Here we change the text";
}
window.onload = function() {
document.addEventListener('click', function (event) {
var target = event.target;
if(target.matches('.answer')){
console.log(target.innerHTML);
}
}, false);
}
</script>
答案 2 :(得分:0)
您需要innerHTML
还是text
?如果您需要文字,请使用.text()
方法。
$(".answer").text("Here we change the text" );
$(".answer").off().on('click',function() {
console.log($(this).html());
});