Ajax请求或加载.js文件中的问题 [英] Problem in Ajax request or load .js file

查看:89
本文介绍了Ajax请求或加载.js文件中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在vs 2010中编写代码,所以每当我通过鼠标访问我页面上的某个链接时,它都会显示一个div标签,它的内容是由来自其他html文件的ajax调用(这些html文件只包含p标签,但它不起作用,即使我试图通过firebug跟踪我的代码,但似乎ajax调用没有请求,或者不能加载我的.js文件



Hi ,I''m trying to write code in vs 2010 ,so whenever I go to one of the links on my page by the mouse ,it can show me a div tag ,which it''s content is loaded by ajax call from other html files (these html files contain only p tags,but It doesn''t work ,even I try to track my code by firebug ,but it seems that there is no request by ajax call ,or can''t load my .js file

<link rel="Stylesheet" type="text/css" href="Styles/MyStyle.css" />
       <script type="text/javascript" src="Previewlinks.js"></script>
    <ul>
        <li><a href="Page1.htm">Page1</a></li>
        <li><a href="Page2.htm">Page2</a></li>
        <li><a href="Page3.htm">Page3</a></li>
        <li><a href="Page4.htm">Page4</a></li>
     </ul>
    <div id="mydiv">
    </div>

#mydiv
{
    background-color:Gray;
    width:150px;
    height:200px;
    font-size:1em;
    font-family:Arial;
    padding:5px;
    position:absolute;
    visibility:hidden;
    border:1px solid Black;
    overflow:hidden;

    }




window.onload = initAll;
var xhr = false;
var xPos, yPos;
function initAll() {
    var allLinks = document.getElementsByTagName('a');
    for (var i = 0; i < allLinks.Length; i++) {
        allLinks[i].onmouseover = MakeRequest;
    }
}
function MakeRequest(evt) {
    if (evt) {
        var url = evt.target;
    }
    else {
        evt = window.event;
        var url = evt.srcElement;
    }
    xPos = evt.clientX;
    yPos = evt.clientY;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    try {
        xhr = new ActiveXObject('Microsoft.XMLHTTP');
    } catch (e) { }
    }
    if (xhr) {
        xhr.onreadystatechange = ShowContents;
        xhr.open('GET', url, true);
        xhr.send(null);
    } else { document.write('sorry,but an error occur in Ajax request'); }
}
function ShowContents() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var outMsg = xhr.responseText;
        } else {
            var outMsg = 'There was a problem' + xhr.status;
        }
        var prevwin = document.getElementById('mydiv');
        prevwin.innerHTML = outMsg;
        prevwin.style.top = parseInt(xPos) + 2 + 'px';
        prevwin.style.left = parseInt(ypos) + 2 + 'px';
        prevwin.style.visibility = 'visible';
        prevwin. önmouseout = function () {
            document.getElementById('mydiv').style.visibility = 'hidden';
        }
    }
}

推荐答案

PreviewLinks.js文件位于何处?在与aspx页面相同的位置?如果不是,你将必须指定完整的相对路径,如:



Where is the PreviewLinks.js file located? In the same location as your aspx page? If not you will have to specify the complete relative path like:

<script type="text/javascript" src="../Scripts/PreviewLinks.js" />





如果您查看浏览器开发人员工具的脚本错误部分,你会发现404错误。这表明Web应用程序无法找到您的.js文件。



即使它不是由于找不到.js文件,脚本窗口将为您提供有关javascript错误的更多信息。发布在这里供有人帮忙...



希望这有帮助!



If you look in to the script errors section of your browser''s developer tools, you will find 404 errors. This indicates that the web application cannot locate your .js file.

Even if it''s not due to the fact that the .js file is not found, the scripts window will give you more information on javascript errors. Post it here for someone to help...

Hope this helps!


功能 MakeRequest( evt)有一个参数 evt 你没有在下面的行中传递任何东西



allLinks [i] .onmouseover = MakeRequest;

我认为那里有一个问题。它可以帮助你



谢谢
Function MakeRequest(evt) have an argument evt and you are not passing any any thing in following line

allLinks[i].onmouseover = MakeRequest;
I think there is a problem in that.hope it helps you

Thanks


我认为''长度''而不是''长度''= for(var i = 0; i< allLinks.Length; i ++){



你可以尝试首先准备好DOM,如果不是当你分配var xPos时,yPos和init上的调用变得不是引用。



如果你没事我给其他一些但是使用jquery:



I think is ''length'' instead of ''Length'' = for (var i = 0; i < allLinks.Length; i++) {

could you try to make the DOM ready first, if not then when you assigned var xPos, yPos and call on init become not reference.

if you`re okay I give some other but using jquery :


这篇关于Ajax请求或加载.js文件中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆