replace anchor text with jquery

后端 未结 5 1071
无人共我
无人共我 2020-12-05 16:45

i want to replace the text of a html anchor:

Click to go home

now i want to replace the

相关标签:
5条回答
  • 2020-12-05 17:21

    To reference an element by id, you need to use the # qualifier.

    Try:

    alert($("#link1").text());
    

    To replace it, you could use:

    $("#link1").text('New text');
    

    The .html() function would work in this case too.

    0 讨论(0)
  • 2020-12-05 17:23

    Try this, in case of id

    $("#YourId").text('Your text');
    

    OR this, in case of class

    $(".YourClassName").text('Your text');
    
    0 讨论(0)
  • 2020-12-05 17:35
    $('#link1').text("Replacement text");
    

    The .text() method drops the text you pass it into the element content. Unlike using .html(), .text() implicitly ignores any embedded HTML markup, so if you need to embed some inline <span>, <i>, or whatever other similar elements, use .html() instead.

    0 讨论(0)
  • 2020-12-05 17:37
    function liReplace(replacement) {
        $(".dropit-submenu li").each(function() {
            var t = $(this);
            t.html(t.html().replace(replacement, "*" + replacement + "*"));
            t.children(":first").html(t.children(":first").html().replace(replacement, "*" +` `replacement + "*"));
            t.children(":first").html(t.children(":first").html().replace(replacement + " ", ""));
            alert(t.children(":first").text());
        });
    }
    
    • First code find a title replace t.html(t.html()
    • Second code a text replace t.children(":first")

    Sample <a title="alpc" href="#">alpc</a>

    0 讨论(0)
  • 2020-12-05 17:39

    Try

    $("#link1").text()
    

    to access the text inside your element. The # indicates you're searching by id. You aren't looking for a child element, so you don't need children(). Instead you want to access the text inside the element your jQuery function returns.

    0 讨论(0)
提交回复
热议问题