在锚标记之前添加了文本

时间:2011-01-26 15:31:16

标签: javascript jquery

我想在我点击的锚标签之前添加一些文字。以下内容将文本添加到实际的锚标记本身:

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

上面给我的html如下所示:

点击链接之前

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

点击链接后

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

我想要的是:

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

6 个答案:

答案 0 :(得分:2)

您可以使用before

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

Live example

答案 1 :(得分:2)

而不是prepend()使用before()

所以它看起来像:

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

答案 2 :(得分:2)

尝试使用before()函数。应该是你需要的

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

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

答案 3 :(得分:1)

尝试使用.before

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

您可以使用.insertBefore,但由于您必须明确创建文本节点,因此它稍微冗长一些:

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

Try them here.

答案 4 :(得分:1)

尝试使用before()代替

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

答案 5 :(得分:1)

使用$.before

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

相关问题