console.log($(this).title="這是第"+(index+1)+"個"+$(this).id);語法錯誤?
<!DOCTYPE html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>Title</title>
? ?<!--jQuery-1.7.2.min版本 官網引用地址如下:-->
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
? ?<script type="text/javascript"src="../js/jquery-2.0.0.min.js"></script>
? ?<script>
? ?$(function()
? ?{
? ? ? ?$("img").each(function(index){
? ? ? ? ? ?console.log($(this).title="這是第"+(index+1)+"個"+$(this).id);
? ? ? ? ? ?alert(index);
? ? ? ?});
? ?})
? ?</script>
</head>
<body>
<img src="../image11/beauty-1.jpg" id="01">
<img src="../image11/beauty-2.jpg" id="02">
<img src="../image11/beauty-3.jpg" id="03">
<img src="../image11/beauty-4.jpg" id="04">
<img src="../image11/beauty-5.jpg" id="05">
</body>
</html>
2018-09-13
$(this)是jquery對象,它沒有title和id屬性,要想調用title和id需將其轉換為DOM對象
$(this)[0].title就可以了
2018-08-10
jquery對象應該是沒有title這個屬性的,
$(this).title
2018-03-12
我的理解,你可以參考一下。$(this)是jq的用法,代表一個集合。在遍歷賦值的時候$(this).id也是一個集合。所以console.log()后,顯示undefined。你改成this.id 就可以正常顯示了。說白了$(this)是{this[0],this[1],this[2],this[3],this[4]}。$(this).id是沒有確定值的。