开发者

Retrieve text from an element and insert it somewhere else

I'd like to get the text from between the "p" tags and put it in an other element, like this:

before:

<div id="Text">
 <p>$1,200.00</p>
</div>

<div id="putText">
 <p></p>
</div>

after:

<div id="Text">
 <p>$1,200.00</p>
</div>


<div id="putText">
 <p>$1,200.00</p>
<开发者_Python百科;/div>

Anyone know of a Javascript that can do this?


The below function copies the contents of the first paragraph under an element with ID ID to a paragraph under another element with ID putID.

function copyContents(id) {
    var source = document.getElementById(id).getElementsByTagName("p")[0];
    var target = document.getElementById("put" + id).getElementsByTagName("p")[0];
    target.innerHTML = source.innerHTML;
}
copyContents("Text");


you can use following jQuery code

$('#putText p').html($('#Text p').html());


If you have jQuery at your disposal, it's fairly easy - something like this should work:

$('#putText>p').text($('#Text>p').text())

If you don't, then you'll have to resort to some DOM manipulation - the same stuff jQuery does behind the scenes, only you need to code it up yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜