简体   繁体   中英

get a specific text of an anchor - jquery

I'd like to select a specific text of an anchor and modify it at it end. I have many unordered lists on a page and I'd like to get a text of an anchor that has a specified href. here is an example:

<ul>
   <li><a href="vue.php?view_id=6">vue 6</a></li>
   <li><a href="vue.php?view_id=7">vue 7</a></li>
</ul>
<ul>
   <li><a href="vue.php?view_id=8">vue 8</a></li>
   <li><a href="vue.php?view_id=9">vue 9</a></li>
</ul>

What if I'd like to get the text of vue 6 for example, using jquery?

$('a[href="vue.php?view_id=6"]')

Here you have the code to play with. Basically is something like this:

Get the Text:

$("ul li a[href='vue.php?view_id=9']").text();

Change the Text:

$("ul li a[href='vue.php?view_id=9']").text("My new text");

Just change the value 'vue.php?view_id=9' for the link you need.

I'd like to select a specific text of an anchor and modify it at it end.

feel the contains() method is pretty useful in this case:

$(function() {
 var $el = $(":contains(vue 6)"),
    text = $el.text(),
    mod = text + " Modification";

  $el.text(mod); // making modification
})

Note: corrected initial response, simplified example code for demonstration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM