2つのコードを考えてください。意図したとおり
1)動作:
$(function(){
$('.menu li').on('click', function(){
var choice = document.getElementById("choice");
var text = this.textContent;
choice.textContent = text;
});
});
2)ここで、$(this)
は "未定義" スロー。
$(function(){
$('.menu li').on('click', function(){
var choice = document.getElementById("choice");
var text = $(this).textContent;
choice.textContent = text;
});
});
私は、時間の長い期間のために選択された要素への参照として$(this)
を使用してきました。しかし、今日は失敗しました。どうしましたか? $(this)
を忘れて、シンプルコードの数行でこのようなケースに直面することはありませんか?
http://stackoverflow.com/questions/3633270/difference-between-this-and-this-in-jqueryあなたが使用する必要があり – Tinmar
: 'VAR文= $(この)は.textを();': ) – Hackerman