使用另一个元素的var word附加html代码

时间:2014-02-13 22:47:36

标签: jquery

我想附加此HTML代码

<script type="text/javascript" src="/feeds/posts/default/-/+++++?published"></script>

到这个div内容

<div class="content"><h>Arts</h></div>

并用<h></h>

之间的文字替换++++

最后结果将是

<div class="content"><h>Arts</h><script type="text/javascript" src="/feeds/posts/default/-/Arts?published"></script></div>

2 个答案:

答案 0 :(得分:1)

var headerText = $(".content").text();
var script = '<script type="text/javascript" src="/feeds/posts/default/-/' + headerText + '?published"></scr' + 'ipt>';
var newText = '<div class="concent"><h>' + headerText + '</h>' + script + '</div>';

$(".content").replaceWith(newText);

http://jsfiddle.net/b7cBF/2/

答案 1 :(得分:0)

给它一个id

<script id="myscript" type="text/javascript" src="/feeds/posts/default/-/+++++?published"></script>

然后

var sauce = $('#myscript').attr('src');
$('#myscript').attr('src', sauce.replace("+++++", $('h').text()));
$('#myscript').appendTo($('div.content'));  

divhttp://jsfiddle.net/b7cBF/

的示例