开发者

Retrieve text from an element and insert it somewhere else

开发者 https://www.devze.com 2023-04-05 00:53 出处:网络
I\'d like to get the text from between the \"p\" tags and put it in an other element, like this: before:

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

精彩评论

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