替换DIV内容Javascript,无法获取正确的外部文件名
我在互联网上找到了这段代码,它似乎可以工作,但无论我将加载到 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
I found this code on the internet and it seems to be working, but no matter what I name the file that will load into the DIV always get the same message that the "Object was not found" What exactly I have to do for the file to get loaded?
This is the HTML Code...
<a href="javascript:void()" onclick="javascript:sendRequest('sourcepage?id=34', 'targetdiv')">Link Text</a>
<div id="targetdiv">This is the target</div>
So... what do I have to name the file for the "sourcepage?id=34" to get it right? So far I have tried "id34.html" "sourcepage-34.html" and similar stuff, but none seems to work.
The script:
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;
}
}
}
I think this is the most stupid question I've done in my life... sorry for that and thanks in advance : D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么您尝试使用的文件名没有扩展名。根据其他服务器的配置,可以将其重新路由到许多不同的文件类型。其中“?id=34”是url参数,与文件名无关。只需替换文件名:
并将文件命名为 myFile.html 并将其放在与上述 HTML 所在的文件夹相同的文件夹中。如果使用 jQuery 是你的一个选择,我推荐它,你可以用这个做你想做的事:
Well the file name that you're trying to use doesn't have an extension. Depending on the other server's configuration, that can be rerouted to a number of different file types. The "?id=34" is the url parameter, nothing to do with the file name. Just replace the file name:
And name your file myFile.html and place it in the same folder as where the above HTML is. If using jQuery is an option for you, I'd recommend it, you could do what you want with this: