简体   繁体   English

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

[英]Replace DIV content Javascript, can't get the external filename right

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? 我在互联网上找到了此代码,它似乎可以正常工作,但是无论我用什么名字命名要加载到DIV中的文件,总会得到与“找不到对象”相同的消息:要加载?

This is the HTML Code... 这是HTML代码...

<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? 那么...为了正确使用该文件,我该如何命名“ sourcepage?id = 34”? So far I have tried "id34.html" "sourcepage-34.html" and similar stuff, but none seems to work. 到目前为止,我已经尝试过“ id34.html”,“ sourcepage-34.html”和类似的东西,但是似乎都没有用。

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 我认为这是我一生中做过的最愚蠢的问题。对此表示歉意,并在此先感谢:D

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. “?id = 34”是url参数,与文件名无关。 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. 并将文件命名为myFile.html并将其放置在与上述HTML相同的文件夹中。 If using jQuery is an option for you, I'd recommend it, you could do what you want with this: 如果您选择使用jQuery,我建议您这样做,您可以使用以下方法进行操作:

$('#targetdiv').load('myFile.html', function(){
    // code to execute once the HTML is loaded into your div
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM