-
ID:0SNJfs さんの質問

画像をクリックするとその画像のsrcをwindow.alertで出したいのですが
Uncaught TypeError: Cannot read property ’src’ of null
となりsrc部分を読み取れなく困っています。
document.getElementById はこういう処理ではあまり用いないのでしょうか? ご存知の方おられましたら教えていただけると嬉しいです。
.
[html]
<img src="http://sample.com/1570840201.jpg" id="js-item-submit">
<img src="http://sample.com/5324250021.jpg" id="js-item-submit">
<img src="http://sample.com/5332000113.jpg" id="js-item-submit">
<img src="http://sample.com/2004002421.jpg" id="js-item-submit">
<img src="http://sample.com/3020401155.jpg" id="js-item-submit">
.
[javascript]
$(document).on(’click’, ’#js-item-submit’, function() {
var title = document.getElementById(’#js-item-submit’).src
window.alert(title);
});

みんなの回答 3 件

ID:SGMQ0Q さんの回答

まず、同じidが複数あるのはなんでなんでしょう・・・?
あとjsとjQueryが混ざっているのが問題ではないかと。
var title = document.getElementById(’#js-item-submit’).src;

var title = document.getElementById(’js-item-submit’).src;
もしくは
var title = $(’#js-item-submit’).attr(’src’);

ID:H8.gY6 さんの回答

むしろ正しい箇所を探すのが難しいくらいのコード
質問の前に、最低限を勉強しようよ

ID:aphaAX

お前がわかんないからそういうんだろう

ID:ZGX9PU さんの回答

複数の要素ならidじゃなくてclassを使いなされ

最終更新日:2015-02-09 (6,805 views)

関連するトピックス

ページ上部に戻る