简体   繁体   English

如何使用JavaScript用链接替换文本?

[英]How to replace text with links using javascript?

This is what I do to take out texts from a html usng Jquery: 这是我从html usng Jquery中提取文本的操作:

$(".text").each(function(){

        var texts = items[textCount]['data']['title'];
        $(this).text(texts);
         textCount = textCount + 1;
      });

My problem if I want the texts to be a url, then it doesn't show up as link but also a string added to the texts variable. 我的问题是,如果我希望文本是url,那么它不会显示为链接,而且还会显示一个添加到texts变量中的字符串。 How do I show a link? 如何显示链接? How can add it as a href to the texts? 如何将其作为href添加到文本中?

Output should be something like this: 输出应该是这样的:

Text + url 文字+网址

and not text being linked as url: "<a href=\\""+link+"\\">"+texts+"</a>" 而不是将文本链接为url: "<a href=\\""+link+"\\">"+texts+"</a>"

Using .attr() function like this. 像这样使用.attr()函数。

$(this).attr("href", "your link");

But this will only work, if you have an anchor tag if not you can create an anchor tag on the fly. 但这只有在您有锚标签的情况下才有效,如果没有锚标签,则可以即时创建锚标签。

$(this).html("<a href=\""+link+"\">"+texts+"</a>");

You can make each element with class=text a link like this which assumes you have some kind of element that you are going to wrap with a hyperlink. 您可以使每个带有class = text的元素成为这样的链接,它假定您具有某种要用超链接包装的元素。 I'm not 100% sure this is what you are looking for. 我不是100%肯定这是您要寻找的。

$('.text').wrap('<a href="http://yourlinkhere.com"></a>');

You can just do. 你可以做。 Note:- this is more efficient than attr() as you are directly dealing with the object property. 注意:-这比attr()更有效,因为您直接处理object属性。

this.href="yoururl"

You do not have to go through the followng jquery code which get called during attr inorder to just set the href attribute. 您不必遍历在attr期间调用的以下jquery代码即可仅设置href属性。

Jquery attr function jQuery attr函数

attr: function( elem, name, value, pass ) {
        var nType = elem.nodeType;

        // don't get/set attributes on text, comment and attribute nodes
        if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
            return undefined;
        }

        if ( pass && name in jQuery.attrFn ) {
            return jQuery( elem )[ name ]( value );
        }

        // Fallback to prop when attributes are not supported
        if ( !("getAttribute" in elem) ) {
            return jQuery.prop( elem, name, value );
        }

        var ret, hooks,
            notxml = nType !== 1 || !jQuery.isXMLDoc( elem );

        // Normalize the name if needed
        if ( notxml ) {
            name = jQuery.attrFix[ name ] || name;

            hooks = jQuery.attrHooks[ name ];

            if ( !hooks ) {
                // Use boolHook for boolean attributes
                if ( rboolean.test( name ) ) {
                    hooks = boolHook;

                // Use nodeHook if available( IE6/7 )
                } else if ( nodeHook ) {
                    hooks = nodeHook;
                }
            }
        }

        if ( value !== undefined ) {

            if ( value === null ) {
                jQuery.removeAttr( elem, name );
                return undefined;

            } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {
                return ret;

            } else {
                elem.setAttribute( name, "" + value );
                return value;
            }

        } else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) {
            return ret;

        } else {

            ret = elem.getAttribute( name );

            // Non-existent attributes return null, we normalize to undefined
            return ret === null ?
                undefined :
                ret;
        }
    },

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

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