2016-05-19 9 views
0

これは、HTMLでの私のコードです:追加(イベントを使用して)「タイトル」の要素に属性

<img id="test" src="https://blah"> 

これは私のjQueryのであり、それは動作します:

私は属性を追加するにはどうすればよい
$(document).on('mouseover', 'img', function(e) { 
} 

この 'img'要素に?私はこれをしたい:

<img id="test" src="https://blah" title='test'> 

は、私が試した:

e.attr('title', 'test'); 
e.prop('title', 'test'); 
this.attr('title', 'test'); 
$(this).attr('title', 'test'); 
this.prop... 

ようにというように。 $(this)はホバーされた画像を表し、この場合

$('img').hover(function() { 
    $(this).attr('title', 'test'); 
}); 

答えて

0

あなたがhover()メソッドを使用することができます。

あなたはまた、行うことができます。この場合

$('img').hover(function(e) { 
    $(e.currentTarget).attr('title', 'foo'); 
}); 

を、$(e.currentTarget)がホバーされた画像を表しています。

+0

私はしたいと思いますが、私はそれが必要です。私はこの 'img'に関するデータを取得するためにこれを使用しています。 –

+0

@SeattleAls私の更新答えを見てください。 – adrianbanks

+0

'e.target.setAttribute( 'title'、 'test');'私はあなたの答えから私を助けました。しかし、私は '$(document).on( 'mouseover'、 'img'、function(e){' –

関連する問題