替换DIV内容Javascript,无法获得正确的外部文件名
Replace DIV content Javascript, can't get the external filename right
我在互联网上发现了这段代码,它似乎正在工作,但无论我如何命名将加载到div中的文件总是得到相同的消息,即"未找到对象"我究竟要为文件做些什么才能加载?
这是HTML代码…
<a href="javascript:void()" onclick="javascript:sendRequest('sourcepage?id=34', 'targetdiv')">Link Text</a>
<div id="targetdiv">This is the target</div>
所以…我该如何命名"sourcepage"的文件?Id =34"才算正确?到目前为止,我已经尝试了"id34.html" "sourcepage-34.html"和类似的东西,但似乎没有工作。
脚本:
function createRequestObject()
{
var returnObj = false;
if(window.XMLHttpRequest) {
returnObj = new XMLHttpRequest();
} else if(window.ActiveXObject) {
try {
returnObj = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
returnObj = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
return returnObj;
}
var http = createRequestObject();
var target;
// This is the function to call, give it the script file you want to run and
// the div you want it to output to.
function sendRequest(scriptFile, targetElement)
{
target = targetElement;
try{
http.open('get', scriptFile, true);
}
catch (e){
document.getElementById(target).innerHTML = e;
return;
}
http.onreadystatechange = handleResponse;
http.send();
}
function handleResponse()
{
if(http.readyState == 4) {
try{
var strResponse = http.responseText;
document.getElementById(target).innerHTML = strResponse;
} catch (e){
document.getElementById(target).innerHTML = e;
}
}
}
我想这是我这辈子做过的最愚蠢的问题…D
你想使用的文件名没有扩展名。根据其他服务器的配置,可以将其重路由到许多不同的文件类型。"?"Id =34"是url参数,与文件名无关。只需替换文件名:
<a href="javascript:void()" onclick="javascript:sendRequest('myFile.html', 'targetdiv')">Link Text</a>
<div id="targetdiv">This is the target</div>
并将文件命名为myFile.html,并将其放置在与上述HTML相同的文件夹中。如果您可以选择使用jQuery,我建议您使用它,您可以使用以下命令:
$('#targetdiv').load('myFile.html', function(){
// code to execute once the HTML is loaded into your div
});
相关文章:
- 调整窗口大小时,可拖动的对象会出现在容器外部
- HTML表单提交时未执行外部函数
- 通过在Dojo mobile ViewController.openExternalView中动态更改打开同一外部视图的
- 使用html中的外部javascript进行数据验证
- TableExport jquery插件:文件名和扩展名问题
- 将变量传递给外部Javascript
- 使用javascript在Flash中加载外部图像
- Firebase2(Firebase.google.com)推送通知-从外部管理
- 如何更改reactjs中外部/独立组件的状态或属性
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- 通过javascript下载文件时设置文件名
- 如何从外部页面激活非默认引导选项卡
- 如何在运行时在angular 2中加载外部js脚本
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 单击元素外部时进行JQuery
- 我的外部js文件无法加载
- 拆下外部分配器
- 什么'在webpack外部设置中,reactDom和reactDom不同
- 将依赖外部库的UMD模块与browserfy捆绑在一起
- 替换DIV内容Javascript,无法获得正确的外部文件名