开发者社区> 问答> 正文

页面加载完成了绑定事件不起作用?

<div id="sV-10" style="display:block">
                            <dl id="sV-10-s1" class="clearfix" >
                                <dt><a href="/search/result.xhtml?licenceId=10">注册建筑工程师</a></dt>
                                <dd><a href="/search/result.xhtml?licenceId=1001">一级</a></dd>
                                <dd><a href="/search/result.xhtml?licenceId=1002">二级</a></dd>
                            </dl>
                            <div class="subCategory subCategoryOne" style="display:none">
                                <div class="subView">
                                    <ul>
                                        <li>
                                        <h3><a href="/search/result.xhtml?licenceId=10">注册建筑工程师</a></h3>
                                            <p>
                                                <a href="/search/result.xhtml?licenceId=1001" class="pointer">一级</a>
                                                <a href="/search/result.xhtml?licenceId=1002" class="pointer">二级</a>
                                            </p>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>

现在想对id为sV-10的绑定一个鼠标划过事件,直接写成

jQuery("#sV-10").hover(function(){
                            jQuery("#sV-10 div").show();
                        },function(){
                            jQuery("#sV-10 div").hide();
                        });
是没问题的,但是现在我这里有sV-10,sV-11......sV-25多个,所以这里我用了个循环,但是绑定就不起作用了,

var tempLic = licence;//.concat(jobLicence);
            if(tempLic && tempLic.length>0){
                var item;
                for(var i=0;i<tempLic.length;i++){
                    item = tempLic[i];
                    if(item && item[3]==1){
                        jQuery("#sV-"+item[2]).hover(function(){
                            jQuery("#sV-"+item[2]+" div").show();
                        },function(){
                            jQuery("#sV-"+item[2]+" div").hide();
                        });
                    }
                }
            }
这段代码鼠标划过又不起作用,不知道为啥?是不是要动态绑定?

展开
收起
a123456678 2016-07-07 14:34:43 2160 0
1 条回答
写回答
取消 提交回答
  • $("div[id^=sV-]").hover(...)

    //不必循环去绑定,一行代码可以搞定,试试看看。检查是否在页面完成后运行的。

    2019-07-17 19:52:48
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载