2012-10-17 88 views
2

我试图用jQuery在一个blockquote元素中创建一个链接。现在我在这个阶段:使用jQuery动态创建链接

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元素,但我想将文本链接块引用的内部。 href不会改变,所以我可以在一个像我已经拥有它的变量中设置它。

我可以在blockquote中添加一些将创建标记的内容,以便我仍然可以使用set变量吗? (这是我一直在试图做的)或者我需要运行这个函数,然后创建一个新的函数来处理添加链接?

回答

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

如果我理解你是对的,你只是想在blockquote中构造一个锚元素并给出文本和链接。

+0

这正是它和完美的作品。谢谢! – erik

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

var blockQ=$('<blockquote class="quote"> 
          <a href="'+hrefLink+'">'+text+'</blockquote>'); 
blockQ.prependTo($this.closest('p')); 

演示:http://jsfiddle.net/QGtYQ/5/