开发者

Variable as jQuery selector focus

开发者 https://www.devze.com 2023-01-31 07:32 出处:网络
I\'m using JavaScript to copy a specific div from a page into a new page.I need to remove the ID attributes for each table in the new page.

I'm using JavaScript to copy a specific div from a page into a new page. I need to remove the ID attributes for each table in the new page.

It seems that since I'm copying content from the first page, I can filter out the IDs from the string before it is written to the second page. Can jQuery take a variable as its 'focus'? Instead of manipulating the entire DOM, manipulate a particular string?

I have a non-working version of what I'm talking about:

var currentContent =  window.open('','currentContentWindow');
var htmlToCopy = '<html><head><title></title></head><body>' + window.frames[0].document.getElementById('PageContentPane').innerHTML + '</body></html>';

        $("table", htmlToCopy).removeAttr('id');

        currentContent.document.open();
        currentConten开发者_如何转开发t.document.write(htmlToCopy);
        currentContent.document.close();


You need to create a jQuery object by calling $(html), manipulate it, then get the HTML back by calling html().

For example:

var currentContent =  window.open('','currentContentWindow');
var htmlToCopy = '<html><head><title></title></head><body>' + window.frames[0].document.getElementById('PageContentPane').innerHTML + '</body></html>';

var newStructure = $("<div>" + htmlToCopy + "</div>");
newStructure.find("table").removeAttr('id');

currentContent.document.open();
currentContent.document.write(newElements.html());

The <div> element allows me to get its inner HTML and get the HTML you're looking for.


Who not just remove ID= as a string and forget DOM manipulation all together?


First make the string a jQuery object, then work with it:

htmlToCopy = $(htmlToCopy).find("table").removeAttr('id').end().html();
0

精彩评论

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