简体   繁体   English

在锚标记之前添加了文本

[英]Added text before an anchor tag

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("test");
    });
});

the above gives me html as shown below: 上面给了我html,如下所示:

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 : 您可以before使用:

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

Live example 现场例子

Instead of prepend() use before() 代替prepend()使用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. 尝试使用before()函数。 Should be what you need 应该是你所需要的

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

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

Try using .before : 尝试使用.before

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

You can use .insertBefore , but it is slightly more verbose since you have to explicitly create a text node: 您可以使用.insertBefore ,但是由于您必须显式创建一个文本节点,因此它有点冗长:

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

Try them here. 在这里尝试。

尝试改用before()

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

使用$.before

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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