我尝试使用jQuery加载方法加载JavaScript:
<div id="update"></div>
<script type="text/javascript">
$('#update').load('find.html .themes', function(data) {
$(this).find('.themes').css({"width":"100%", "margin":"auto"});
});
</script>
我的文件find.html:
<div class="themes">
<script> document.write('This is a test'); </script>
</div>
有人可以告诉我如何加载JavaScript吗?
答案 0 :(得分:0)
然后你应该使用其他ajax mehtod,例如:
$.get('find.html', function(data) {
$('#update').empty().append($('<div/>', {
html: data
}).find('.themes').css({
"width": "100%",
"margin": "auto"
}));
});
empty().append()
不html()
只接受字符串(即使你可以传递对象,但它不是为它而设计的,可以 它是车)。
PS:请注意,在您的情况下,使用document.write()
会覆盖所有document
内容,因为这是文档完全解析后的工作方式。所以不要使用它,即使只是为了测试目的。
答案 1 :(得分:0)
如果您的find.html
div中没有太多东西,那么您可以直接访问所有元素:
<script type="text/javascript">
$('#update').load('find.html', function(data) {
$(this).find('.themes').css({
"width": "100%",
"margin": "auto"
});
});
</script>