jquery按标题查找href

时间:2015-06-17 13:55:11

标签: javascript jquery html css

我有侧面菜单,如下所示。在点击href链接时,我通过href title显示相应的div id。

<ul class="sidenav">
    <li><a href="#" title="sample">Sample</a></li>
    <li><a href="#" title="test">Test</a></li>
    </ul>

我还有下一个按钮,点击它时,我想突出显示当前div显示的href。 所以我有下面的id,

var $title = $(".hidefields:visible").attr('id'); //this is the href title and hidefields is the div class 
$("a[title|= $title]").css("color", "red");..// this doesn't work.

如何通过变量标题找到href?我无法在互联网上找到解决方案。

3 个答案:

答案 0 :(得分:2)

使用attribute选择器

    <style name="DropDownStyle">

   <item name="android:layout_width">0dp</item>

您的代码

$('a[title="sample"]')

答案 1 :(得分:0)

你几乎拥有它,但你的$title被视为一个字符串。所以做一些像:

$("a[title|= " + $title + "]").css("color", "red");

答案 2 :(得分:0)

我建议您尝试使用类属性而不是标题来查找它。没有什么说他们不能拥有相同的价值,但jQuery是建立在使用类和ID来寻找DOM中的节点的基础上的。

EG。从你的问题上面: HTML:

<ul class="sidenav">
     <li><a href="#" class="sample" title="sample">Sample</a></li>
     <li><a href="#" class="test" title="test">Test</a></li>
</ul>

jQuery的:

var $title = $(".hidefields:visible").attr('id'); //this is the href title and hidefields is the div class 
$(".hidefields:visible a." + $title).css("color", "red");
相关问题