George_Fal 2012-10-03 07:18 采纳率: 0%
浏览 26

jQuery多个加载到div中

I am trying to make a page which has 4 configurable sections all of which will provide the user with the same inputs but ultimately be 4 unique configuration settings.

I am using a generic base page to do this but have run into problems with Jquery selectors not being able to tell the difference between the sections inside different divs but all originating from the same page. At first i thought this was implmentation specific so have created a smaller test page but ultimately seem to have the same problem.

test.html is as follows:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="jquery/js/jquery-ui-1.8.24.custom.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function () 
{
    $('#div1').load("test2.html");
    $('#div2').load("test2.html");
});
</script>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>

test2.html is as follows:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="jquery/js/jquery-ui-1.8.24.custom.min.js"></script>
</head>
<body>
<script type="text/javascript">
function testing()
{
    alert("Alert: " + $('#test').val());
}
</script>
<div id="testing">
<input id="test" onchange="testing()"></input>
</div>
</body>
</html>

If this is not the right way to do this what would be a better way, i understand i could probably use iframes but will i be able to access the values from the input box's in the page housing the others.

I have tested this on ubuntu firefox 15.0.1

Thanks.

  • 写回答

2条回答 默认 最新

  • weixin_33694620 2012-10-03 07:25
    关注

    First of all, you should not be using a full HTML document with jQuery's load() method. You should only have the contents of the div inside. E.g. no doctype, no html or body tag. Just the body itself.

    Secondly: element identifiers should be unique on any page. When you load the content of the first div, it creates a div element with id=testing. When you load it the second time, it tries to create another copy of it, but with the same identifier.

    Identifiers must be unique. (This is how they identify...)

    Alternative solution would be to load the content with ajax(), then replace the identifier from Javascript code, and finally call $("#div1").html(replaced_html_content). But if I were you, I wouldn't do that. Use server side scripting and provide query parameters instead.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能