在ajax中加载javascript时遇到问题

在ajax中加载javascript时遇到问题,javascript,ajax,jquery,dom,Javascript,Ajax,Jquery,Dom,我正在开发网站,我需要用ajax更改页面。每个页面都有不同的javascript和css。我在加载ajax内容时调用javascript时出错了。此外,我在加载ajax内容时成功调用了警报框,但在加载原始脚本时遇到了问题。 这是我的一些代码片段, index.html页面脚本 <script type="text/javascript"> function marmi(){ jQuery(document).ready(function(){ //resiz

我正在开发网站,我需要用ajax更改页面。每个页面都有不同的javascript和css。我在加载ajax内容时调用javascript时出错了。此外,我在加载ajax内容时成功调用了警报框,但在加载原始脚本时遇到了问题。 这是我的一些代码片段, index.html页面脚本

<script type="text/javascript">
function marmi(){

    jQuery(document).ready(function(){
        //resizeDiv();
    });
    window.onload = function(event) {
        resizeDiv();
    }
    function resizeDiv() {
        vph = jQuery(window).height();
        mamu = (jQuery(window).height() - 277);
        jQuery('#mam').css({'height': mamu + 'px'});

    }
}
</script>
<script type="text/javascript">
function successCallback() {
  function marmi(){

    jQuery(document).ready(function(){
        resizeDiv();
    });
    window.onload = function(event) {
        resizeDiv();
    }
    function resizeDiv() {
        vph = jQuery(window).height();
        mamu = (jQuery(window).height() - 277);
        jQuery('#mam').css({'height': mamu + 'px'});

    }
}
}

function completeCallback() {
    jQuery(document).ready(function(){
        resizeDiv();
    });
    window.onload = function(event) {
        resizeDiv();
    }
    function resizeDiv() {
        vph = jQuery(window).height();
        mamu = (jQuery(window).height() - 277);
        jQuery('#mam').css({'height': mamu + 'px'});

    }
    alert('comleted');
}

function errorCallback() {
    alert('error');
}

jQuery.ajax({
    url:"index.html",
    success:successCallback,
    complete:completeCallback,
    error:errorCallback

});
</script>
我正在这个div中加载ajax内容

<div id="ajax">

</div>
控制台输出

href is: index.html 
href is: about.html 
href is: contact.html 
href is: index.html 
href is: about.html 
href is: contact.html
修改的index.html页面脚本

<script type="text/javascript">
function marmi(){

    jQuery(document).ready(function(){
        //resizeDiv();
    });
    window.onload = function(event) {
        resizeDiv();
    }
    function resizeDiv() {
        vph = jQuery(window).height();
        mamu = (jQuery(window).height() - 277);
        jQuery('#mam').css({'height': mamu + 'px'});

    }
}
</script>
<script type="text/javascript">
function successCallback() {
  function marmi(){

    jQuery(document).ready(function(){
        resizeDiv();
    });
    window.onload = function(event) {
        resizeDiv();
    }
    function resizeDiv() {
        vph = jQuery(window).height();
        mamu = (jQuery(window).height() - 277);
        jQuery('#mam').css({'height': mamu + 'px'});

    }
}
}

function completeCallback() {
    jQuery(document).ready(function(){
        resizeDiv();
    });
    window.onload = function(event) {
        resizeDiv();
    }
    function resizeDiv() {
        vph = jQuery(window).height();
        mamu = (jQuery(window).height() - 277);
        jQuery('#mam').css({'height': mamu + 'px'});

    }
    alert('comleted');
}

function errorCallback() {
    alert('error');
}

jQuery.ajax({
    url:"index.html",
    success:successCallback,
    complete:completeCallback,
    error:errorCallback

});
</script>

函数successCallback(){
函数marmi(){
jQuery(文档).ready(函数(){
resizeDiv();
});
window.onload=函数(事件){
resizeDiv();
}
函数resizeDiv(){
vph=jQuery(window).height();
mamu=(jQuery(window).height()-277);
jQuery('#mam').css({'height':mamu+'px'});
}
}
}
函数completeCallback(){
jQuery(文档).ready(函数(){
resizeDiv();
});
window.onload=函数(事件){
resizeDiv();
}
函数resizeDiv(){
vph=jQuery(window).height();
mamu=(jQuery(window).height()-277);
jQuery('#mam').css({'height':mamu+'px'});
}
警报(“已完成”);
}
函数errorCallback(){
警报(“错误”);
}
jQuery.ajax({
url:“index.html”,
成功:成功,
完成:完成回调,
错误:errorCallback
});

我假设它在代码的以下部分,您可以添加建议的console.log并检查控制台中的xhr活动吗?您可以将Firefox与firebug插件或Chrome一起使用。按F12键查看控制台

jQuery(document).ready(function() {
    var hash = window.location.hash.substr(1);
    var href = jQuery('.aj_me').each(function() {
        var href = jQuery(this).attr('href');
        console.log("href is:",href);
        if (hash == href.substr(0, href.length - 5)) {
            var toLoad = hash + '.html #ajax';
            console.log("Going to load url:",toLoad);
            jQuery('#ajax').load(toLoad)
        }
    });

我认为您需要看看jQueryAjax文档

如果希望Javascript代码在AJAX调用成功完成后运行,请将该代码放在下面的成功部分

$.ajax({
    type: "POST",
    url: '/login/process',
    data: formData,
    success: function (data) 
    {
        // Javascript to run
    }
});

如果查看jQuery的source()和load函数,就会发现当您指定要从文档中提取的选择器时,它会使用$.parseHTML()解析选择器的内容

self.html(选择器?
//如果指定了选择器,请在虚拟div中找到正确的元素
//排除脚本以避免IE“权限被拒绝”错误
jQuery(“”).append(jQuery.parseHTML(responseText)).find(选择器):
默认情况下,$.parseHTML会阻止脚本,但可以通过将“true”作为第二个参数传递给parseHTML函数来覆盖脚本

长话短说……如果您想运行脚本,您可以使用自己的自定义ajax函数来代替load

var toLoad = '#ajax';

$.ajax({
    url: yourURL,
    dataType: 'html',
    success: function (response) {
       var content = $('<div>').append($.parseHTML(response), true).find(toLoad);
       $('#ajax').html(content[0]);
    }
});
var-toLoad='#ajax';
$.ajax({
url:yourURL,
数据类型:“html”,
成功:功能(响应){
var content=$('').append($.parseHTML(response),true.find(toLoad);
$('#ajax').html(内容[0]);
}
});

您应该提供一个最简单的完整示例,说明失败的原因和您的期望,目前的上述问题很难确定您要修复的问题。我的脚本存在冲突,因此我使用jQuery而不是$i know,但我无法按照我的公司政策提供实时链接。啊,够公平了@simpleclickI see一个错误。这个
jQuery('#ajax').load(toLoad',showNewContent())
应该是:
jQuery('#ajax').load(toLoad',showNewContent)
@jfriend00不幸的是,我修改了它,但它对我不起作用,所以它应该是不同的错误正如你所说,只是修改了我的ajax脚本,并在上面添加了控制台输出,似乎浏览器只获取html内容,其他什么都没有他使用的
.load()
它有自己的完成功能。我不认为这是OP的问题所在?我认为他可以从重构代码和使用JQuery的$notation中获益。如果它更具可读性,它可能会帮助社区更好地理解他的问题并提供解决方案。我不能使用$If,如果我使用的比我的脚本冲突和它更糟糕很遗憾,没有。当我在success中使用警报功能时,我可以得到警报。你可以检查我刚才上传的代码。如果你使用的是Google Chrome或FireFox,你可以放置console.log('hello');将消息记录到输出窗口。也许也可以查看一下您正在调用的页面,以确保它没有失败?您可以尝试Fiddler进行调试。请记住,如果这样做,您可能会在IE中遇到权限错误(如上面从jQuery源复制的注释中指定的)。
 self.html( selector ?

    // If a selector was specified, locate the right elements in a dummy div
    // Exclude scripts to avoid IE 'Permission Denied' errors
    jQuery("<div>").append( jQuery.parseHTML( responseText ) ).find( selector ) :
var toLoad = '#ajax';

$.ajax({
    url: yourURL,
    dataType: 'html',
    success: function (response) {
       var content = $('<div>').append($.parseHTML(response), true).find(toLoad);
       $('#ajax').html(content[0]);
    }
});