簡體   English   中英

jQuery按標題查找href

[英]jquery find a href by title

我的菜單如下所示。 單擊href鏈接后,我將按href標題顯示相應的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? 我在互聯網上找不到解決方案。

使用屬性選擇器

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

您的密碼

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

您幾乎擁有了它,但是$title被視為一個字符串。 因此,請執行以下操作:

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

我建議您嘗試使用類屬性而不是標題來查找它。 沒有什么說它們不能具有相同的值,但是jQuery是使用類和ID在DOM中查找節點的基礎上構建的。

例如。 從上面的問題中詢問: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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM