Home > Web Front-end > JS Tutorial > body text

Solutions to problems with the jquery selector [attribute=value]

黄舟
Release: 2017-06-23 13:48:19
Original
1225 people have browsed it

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);
       });
Copy after login

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 + "']"
Copy after login

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>
    $(&#39;img&#39;).mouseenter(function() {        var bigimgsrc = $(this).attr(&#39;src&#39;),
            liindex = $(this).siblings(&#39;ul&#39;).children("[data-src=&#39;" + bigimgsrc + "&#39;]").index()
        alert(liindex );
    });    </script>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template