简体   繁体   中英

Remove tag and content using Javascript

What I want is to get the content of a specific #id , remove a few tags and its contents, add some html before and after it, and then set that finished content as the body content.

I now have the following code, containing a mixture of Javascript and jQuery, although obviously not the right one - resulting in a [object Object] -message.

My code looks like this:

var printContents = jQuery("#"+id).clone();
var printContentsBefore = '<html><head></head><body><table><tr>';
var printContentsAfter = '</tr></table></body></html>';
var mainContents = printContents.find(".td_print").remove();

document.body.innerHTML = printContentsBefore + mainContents + printContentsAfter;

Any ideas of how to make this work?

Your code does not convert the cloned jquery object to a string. Modify your code as follows:

document.body.innerHTML = printContentsBefore + mainContents.html() + printContentsAfter;

Beware that the .html method output will not include the html representation of the container element itself (ie. of the #id clone in your case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM