用jQuery动态创建链接 [英] Dynamical Creating Links with jQuery

查看:173
本文介绍了用jQuery动态创建链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery在blockquote元素内创建链接.现在,我处于这个阶段:

I'm trying to create a link within a blockquote element with jQuery. Right now I'm at this stage:

var pullQuote = $('span.pull-quote').each(function(){

    var $this = $(this),
    hrefLink = 'http://example.com',
    text = $this.text();

    $('<blockquote>', { 
        class: 'quote',
        text: text 
    }).prependTo( $this.closest('p'));

});

这将创建具有动态文本的blockquote元素,但我想将文本转换为blockquote内部的链接. href不会改变,因此我可以像已经拥有的那样在变量中进行设置.

That creates the blockquote element with the text dynamical but I want to turn the text into link inside of the blockquote. The href isn't going to change so I can set that in a variable like I already have it.

我可以添加一些可以在blockquote内创建标记的地方,让我仍然可以使用set变量吗? (这是我一直想做的事情)还是只需要运行此函数然后创建一个新函数来处理添加链接的工作?

Can I add something that will create the a tag inside of the blockquote to where I can still use the set variables? (Which is what I've been trying to do) Or do I need to just run this function and then create a new function which would handle adding the link?

推荐答案

$('<blockquote/>', { 
    class: 'quote',
    html: $('<a/>', {
        text: text,
        href: hrefLink
    )}
}).prependTo( $this.closest('p'));

如果我理解正确,那么您只是想在blockquote中构造一个锚元素,并给出文本和链接.

If I understood you right, you just wanted to construct an anchor element within the blockquote and give that the text and the link.

这篇关于用jQuery动态创建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆