純粋なjavascriptのmap()関数の中に$(this)jQueryセレクタを複製しようとしています。 私はliのonclickを聞き、その特定のliに背景イメージを適用したいと思います。
は、ここに私のコードですが、ここでは、「この」クリックしたli要素は戻りません:
var squares = document.getElementsByClassName('box'); //returns an HTML Collection of 9 lis
function myEvent(event) {
squares = Array.prototype.slice.call(squares); //converts HTML collection to an Array
squares.map(function(x, index) {
if (event.type === "click") {
this.style.backgroundImage = 'url(img/' + app.imagePath() + '.svg)';
}
}, this);
}
squares.addEventListener("click", myEvent(), false);
任意の提案を?
あなたはタイプのシナリオを "動作しない" のではなく、あなたが持っている特定のエラーを説明してもらえますか? –
http://www.w3schools.com/js/js_htmldom_eventlistener.asp – Yuri
クリックイベントをどのようにLiにバインドしていますか? –