替换DIV内容Javascript,无法获取正确的外部文件名

发布于 2024-11-26 03:46:40 字数 1677 浏览 5 评论 0原文

我在互联网上找到了这段代码,它似乎可以工作,但无论我将加载到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

得不到的就毁灭 2024-12-03 03:46:40

那么您尝试使用的文件名没有扩展名。根据其他服务器的配置,可以将其重新路由到许多不同的文件类型。其中“?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
});

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:

<a href="javascript:void()" onclick="javascript:sendRequest('myFile.html', 'targetdiv')">Link Text</a>
<div id="targetdiv">This is the target</div>

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:

$('#targetdiv').load('myFile.html', function(){
    // code to execute once the HTML is loaded into your div
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文