jqueryProblem with attribute selector[attribute=value]
$("img").mouseenter(function (){ var bigimgsrc = $(this).attr('src'); var liindex = $(this).siblings("ul").children("[datasrc=bigimgsrc]").index(); alert(liindex); });
The problem is: liindex is always -1;
So what’s wrong with my var liindex = $(this).siblings("ul").children("[datasrc=bigimgsrc]").index();? Please give me some advice.
"[datasrc='" + bigimgsrc + "']"
I am usually used to using quotation marks, but I didn’t see it clearly at first. Variables You should use splicing.
The grammar is not wrong, check if there are any spelling errors. index() returns -1 if the element cannot be found, so is there a typo in the selector for children?
<div> <img src="http://apeclass.cn/1212/img/photo_01.jpg"> <ul> <li data-src="http://apeclass.cn/1212/img/photo_01.jpg"></li> <li></li> <li></li> </ul> </div> <div> <img src="http://apeclass.cn/1212/img/photo_02.jpg"> <ul> <li></li> <li data-src="http://apeclass.cn/1212/img/photo_02.jpg"></li> <li></li> </ul> </div> <div> <img src="http://apeclass.cn/1212/img/photo_03.jpg"> <ul> <li></li> <li></li> <li data-src="http://apeclass.cn/1212/img/photo_03.jpg"></li> </ul> </div> <script> $('img').mouseenter(function() { var bigimgsrc = $(this).attr('src'), liindex = $(this).siblings('ul').children("[data-src='" + bigimgsrc + "']").index() alert(liindex ); }); </script>
Explanation $(this).siblings("ul").children("[datasrc=bigimgsrc]") did not select any elements.
The above is the detailed content of Solutions to problems with the jquery selector [attribute=value]. For more information, please follow other related articles on the PHP Chinese website!