简体   繁体   中英

How to append a div using jquery

无论如何,我可以通过从其他页面(例如,其他页面)或正文中的某个位置附加“ div #B”来实现以下目的,而不是在脚本本身内部编写它,并且不会遇到诸如使用JQuery的Load时遇到的相同源策略问题?

$('#A').append("<div id='B'><span><img src='i1.png'></span></div>");

You can use this to append an element's HTML in your body

$('#A').append($('#someelement').html());

This won't work with cross origin iframes because of same-origin policy. But you can get the HTML from another page by using AJAX, as long as the URL is in the same origin.

I'm sorry but if you wish to append a div, you must be able to specify it's contents and styles.

If you want to extract it from a div on the page do this:

var loadAble = $('#YourDivName').html();
$('#A').append("<div class="B"></div>");
$('#A .B').html(loadAble);

Remember to replace YourDivName respectively!

I know you said not to use .load(), but here goes anyway:

$('#A').append("<div class="B"></div>");
$('#A .B').load("yourFileGoesHere.html#YourDivName");

An alternative (untested) is to write:

var B = $('#A').append("<div class="B"></div>");
B.load("yourFileGoesHere.html#YourDivName");

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