1

嘿伙计们,对不起我糟糕的英语前锋。

我有这个代码:

<body>
    <div id="left">
    <p><a name = "container" value="topright" href="javascript:handleContent('topright')" onclick="loadContent('hallo1.php');">Hallo Welt 1</a></p>               
    <p><a name = "container" value="bottomright" href="javascript:handleContent('bottomright')" onclick="loadContent('hallo2.php');">Hallo Welt 2</a></p>
    </div>
    <div id="topright"></div>
    <div id="bottomright"></div>
</body>

和阿贾克斯:

    var xmlHttpObject = false;

if (typeof XMLHttpRequest != 'undefined') 
{
    xmlHttpObject = new XMLHttpRequest();
}
if (!xmlHttpObject) 
{
    try 
    {
        xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) 
    {
        try 
        {
            xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e) 
        {
            xmlHttpObject = null;
        }
    }
}

function loadContent(id){    
    xmlHttpObject.open('get', id);
    xmlHttpObject.onreadystatechange = handleContent;
    xmlHttpObject.send(null);

    return false;
}

function handleContent(){
    if (xmlHttpObject.readyState == 4)

    {
        document.getElementById('topright').innerHTML = xmlHttpObject.responseText;
    }
}

如果我单击“Hallo Welt 1”,我想在右上角的 div 中回显 Hallo1.php。因此,如果我单击“Hallo Welt 2”,我想在右下角的 div 中回显 Hallo2.php。我的 Ajax 代码中有问题。请帮助我,我是新人:)

谢谢转发

4

1 回答 1

0

基本问题是如何将参数传递给 handleContent。href="" 当前不执行任何操作。解决方案是通过闭包传递“topright”或“bottomright”。像这样工作:

(function (t){
    return function(){
        handleContent(t);
    }
})(target);

这将创建一个直接调用的函数并返回一个“绑定”您的参数的函数(这称为闭包)。

我认为你的德语:Du musst dafür sorgen das deine Argumente Richtig übertragen werden。Das machst du am einfachsten mit einem sogenannten Closure(eine Funktion die deinen Parameter in sich selbst speichert)。

解决方案(在 Firefox 中测试):

<body>
    <div id="left">
    <p><a name = "container" value="topright" onclick="loadContent('hallo1.php', 'topright');">Hallo Welt 1</a></p>               
    <p><a name = "container" value="bottomright" onclick="loadContent('hallo2.php', 'bottomright');">Hallo Welt 2</a></p>
    </div>
    <div id="topright"></div>
    <div id="bottomright"></div>
    <script>

var xmlHttpObject = false;

if (typeof XMLHttpRequest != 'undefined') 
{
    xmlHttpObject = new XMLHttpRequest();
}
if (!xmlHttpObject) 
{
    try 
    {
        xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) 
    {
        try 
        {
            xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e) 
        {
            xmlHttpObject = null;
        }
    }
}

function loadContent(id, target){    
    xmlHttpObject.open('get', id);
    xmlHttpObject.onreadystatechange = (function(t){return function(){ return         handleContent(t);}})(target);
    xmlHttpObject.send(null);

    return false;
}

function handleContent(target){
    if (xmlHttpObject.readyState == 4)

    {
        document.getElementById(target).innerHTML = xmlHttpObject.responseText;
    }
}
</script>
</body>
于 2013-08-19T10:17:12.640 回答