jQuery条件不匹配

时间:2013-11-29 05:11:54

标签: c# jquery html asp.net

我有以下HTML

<div id='cssmenu'>
<ul>
 <li class= 'has-sub'><a href=store.aspx?id=dLYTWvt8EsHOq7Ps2wJA9A%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat</span></a> <ul>
 <li class= 'has-sub'><a href=store.aspx?id=xEDEzZWDRkX8%2fbXoMX2pSQ%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-2</span></a><ul>
 <li class= 'active'><a href=store.aspx?id=TxlSRrnSZ6HDgj%2b2ZSxRhg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance -1</span></a></li>
 <li class= 'active'><a href=store.aspx?id=Y7JxNAXSuG1T%2f0cpjQXSwA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance - 2</span></a></li>
 <li class= 'active'><a href=store.aspx?id=%2fcoJMcJTjWp3%2bPuimR5AhA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-5</span></a></li>
</ul></li> <li class= 'has-sub'><a href=store.aspx?id=KC2g4igBzXisbsbKu%2fKrzw%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-3</span></a><ul>
 <li class= 'active'><a href=store.aspx?id=8DCHnP%2b4KQVYDTGxVt9snQ%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Death -1</span></a></li>
 <li class= 'active'><a href=store.aspx?id=nGEm4rbNMQ%2bQfyO44ECmpA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Death - 1</span></a></li>
</ul></li> <li class= 'has-sub'><a href=store.aspx?id=feki%2bXDs66obnO1e2dxHWg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-4</span></a></li>
 <li class= 'has-sub'><a href=store.aspx?id=5O394Lww9oxD1vHbIY7LWw%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-6</span></a></li>
</ul> </li> <li class= 'active'><a href=store.aspx?id=RIe63RBggHzj76SUwDHKMg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance</span></a></li>
</ul>
</div>

并在reqid

 var reqid = "<%=Request["id"]%>";

reqid中,我会根据菜单选择

获取urlencoded

我的jQuery代码

$(function () {
    var str = $("#cssmenu").find('li').find('a').attr('href');

    if ($(str).has(reqid)) {    
        var str2 = $("#cssmenu").find('li').find('a').text();
        $('#selectedmenuitem').html(str2);
    }
});

如果href属性编码的url包含selectedur的所选菜单项,我想将匹配的url .text()添加到#selectedmenuitem标签id。

我检查了包含,但没有得到。请建议我有什么其他可能性。

2 个答案:

答案 0 :(得分:0)

使用Javascript的indexOf函数

http://www.w3schools.com/jsref/jsref_indexof.asp

答案 1 :(得分:0)

使用以下jquery代码

$(function () {
    var str2= $("#cssmenu").find("li").find("a[href*='" + reqid + "']").text();
    $('#selectedmenuitem').html(str2);
});