开发者

Added text before an anchor tag

开发者 https://www.devze.com 2023-02-06 16:54 出处:网络
I would like to add some text just before the anchor tag I click on.The following adds the text to the actual anchor tag itself:

I would like to add some text just before the anchor tag I click on. The following adds the text to the actual anchor tag itself:

$(document).ready(function(){
    $(".none_standard_links").live('click', function(event)
    {
        event.preventDefault();
        $(this).prepend("te开发者_运维百科st");
    });
});

the above gives me html as shown below:

Before the link is clicked

<a href="#" class="none_standard_links">link one</a>

After the link is clicked

<a href="#" class="none_standard_links">testlink one</a>

What I want is this:

test<a href="#" class="none_standard_links">link one</a>


You can use before:

$(document).ready(function(){
    $(".none_standard_links").live('click', function(event)
    {
        $(this).before("test");
    });
});

Live example


Instead of prepend() use before()

So it would look like:

$(document).ready(function(){
    $(".none_standard_links").live('click', function(event)
    {
        event.preventDefault();
        $(this).before("test");
    });
});


Try using the before() function. Should be what you need

$(this).before("test");

http://api.jquery.com/before/


Try using before() instead

 $(this).before("test");


Use $.before:

$(this).before("test");


Try using .before:

$(this).before('something');

You can use .insertBefore, but it is slightly more verbose since you have to explicitly create a text node:

$(document.createTextNode("hello")).insertBefore("a");

Try them here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号