/ / IE8 'attr(...)'がnullかオブジェクトではない - jquery、placeholder、attr

IE8 'attr(...)'がnullかオブジェクトではない - jquery、placeholder、attr

私はjquery IEのプレースホルダの修正プログラムを取得しようとしています。IE9では問題なく動作しますが、IE8では警告が表示され続けます...

"attr(...)"はnullまたはオブジェクトではありません

これは私が使用しているコードです。私は何か間違っているのですか?

<!--[if lte IE 9 ]>
<script>
// IE Placeholder Fix
$(function() {
if ($("input[placeholder]").attr("placeholder").val() != "") {
var ph = $("input[placeholder]").attr("placeholder");
$("input[placeholder]").blur();
if ($("input[placeholder]").attr("value").val() == 0) {
$("input[placeholder]").attr("value", ph);
$("input[placeholder]").css("color","#999999");
};
$("input[placeholder]").click(
function() {
$(this).css("color","#000000");
if ($(this).attr("value") == ph) {
$(this).attr("value","");
}
});
}
});
</script>
<![endif]-->

更新:

以下のコメントを使って私は思いついた。

<!--[if lte IE 9 ]>
<script>
// IE Placeholder Fix
$(function() {
if ($("input[placeholder]").length != 0) {
var ph = $("input[placeholder]").attr("placeholder");
$("input[placeholder]").blur();
if ($("input[placeholder]").attr("value") == "") {
$("input[placeholder]").attr("value", ph);
$("input[placeholder]").css("color","#999999");
};
$("input[placeholder]").click(
function() {
$(this).css("color","#000000");
if ($(this).attr("value") == ph) {
$(this).attr("value","");
}
});
}
});
</script>
<![endif]-->

回答:

回答№1は1

ヌルオブジェクトでメソッドまたはプロパティを呼び出すと、発生しているソートにエラーが発生します。オブジェクトが定義されているかどうかわからない場合は、子を参照する前にこれをテストしてください。