2014-03-07 91 views
0

因此,我正在研究点击图片时弹出的幻想扩展器的事情 - 想想点击谷歌图片搜索结果时弹出的东西(我没有写出大量的代码),它是设立这样的每个元素:jQuery,如何在创建元素时嵌套HTML标签?

<a href="http://i.local.dev:5000/EW3GugE.jpg" 
    data-largesrc="http://i.local.dev:5000/EW3GugE.jpg" 
    data-title="http://i.local.dev:5000/EW3GugE.jpg"/> 
<div class = "thumbcontainer">    
    <img src="http://local.dev:5000/thumbs/EW3GugE.jpg" /> 
</div> 

“数据标题”属性被由JavaScript评价,jQuery的代码创建这样的元件,同时,这是在标题属性中的文本,并把它的内部一个textarea,然后将其附加到弹出框中:

this.$title = $('<textarea></textarea>'); 

this.$details = $('<div class="og-details"></div>').append(this.$title, 
                this.$description, this.$href); 
this.$details = $('<div class="og-details"></div>').append(this.$title, 
                this.$description, this.$href); 

这工作正常,它将文本放入文本框中。但我需要做的是进行一些设置高亮/各种其他的标记链接,类似于:

[IMG] http://i.imgur.com/5VbkHIP.jpg [/ IMG]

没有认为这会工作,但我试过:

this.$title = $('<textarea>[IMG][IMG]</textarea>'); 

它没有工作。有没有一种方法可以指定这些图片标签之间的文字?我应该将它们评估为字符串并将它们结合起来,还是这是一种不好的方法?谢谢

回答

0

您是否尝试过使用此代码设置textarea的值?

this.$title.val('[IMG][/IMG]'); 

祝你好运。 :)

+0

不工作 - 现在所有这些都是在文本框中是[IMG] [/ IMG],链接不它们之间。 – jack

0

假设“这一点。$ HREF”是HTML的jQuery对象

<a href="http://i.local.dev:5000/EW3GugE.jpg" data-largesrc="http://i.local.dev:5000/EW3GugE.jpg" data-title="http://i.local.dev:5000/EW3GugE.jpg"/> 

我想尝试直接修改属性“数据所有权”的价值,施工前的$细节对象,这样标题文本将被包裹在“[IMG]”标签之间。因此,它会是什么样子,

var myText = this.$href.attr("data-title"); 
this.$href.attr("data-title", "[IMG]" + myText + "[/IMG]"); 

然后,就

this.$details = $('<div class="og-details"></div>').append(this.$title, this.$description, this.$href);