我一定是做了一些愚蠢的事情,但我真的不明白,为什么这个脚本不是console.log(ing) <p>
中的文本标签?
HTML:
<div>
<label>Display Name:</label>
<div>
<p>The room that its in.</p>
<span class="edit"></span>
</div>
</div>
jQuery:
if ($('span.edit').length > 0) {
$(this).click(function(){
var elem = $(this).prev();
var text = elem.text();
console.log(text);
elem.html('<input type="text">' + text + '</input>');
});
}
最佳答案
JavaScript 不是 block 作用域;您期望这个
是什么?
已修复:
if ($('span.edit').length > 0) {
$('span.edit').click(function(){
var elem = $(this).prev();
var text = elem.text();
console.log(text);
elem.html('<input type="text" value="' + text + '" />');
});
}
编辑:更新了html()
内容以纠正输入错误
关于jQuery this() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15688335/