javascript - JQuery 函数被调用,未执行并且开发控制台中没有错误

标签 javascript jquery

我的页面有一系列带有 onclick 事件的按钮,用于调用名为 ShowCenter() 的 jQuery 函数,该函数用内容填充 div。这已经工作了很长时间:

<div class="C1"><button class="button_01" id="first" onclick="ShowCenter(1); ShowFarRight(150);">Button Text Here</button></div>

今天,我添加了一个新的函数 ShowFarRight(),以便在调用第一个函数后立即填充另一个 div:

<script>
function ShowFarRight(type) {
  console.log("Far Right-1");
  $(" #C3 ").empty();
  $(" #C3 ").scrollTop();
  var filename = "text_" + type + ".htm"
  $( "#C3" ).hide().load( filename ).fadeIn(500);
  console.log("Far Right-2");  }
</script>

问题是 ShowFarRight 不执行任何操作,并且 Firefox 和 Chrome 开发控制台不显示 console.log 消息或任何其他错误。

编辑:这是 ShowCenter 的代码

<script>
var CurrentPageLoad
function ShowCenter(type) {
    $(" #C2 ").empty();
    $(" #C2 ").scrollTop();
    var filename = "text_" + type + ".htm"
    $( "#C2" ).hide().load( filename ).fadeIn(500);
    CurrentPageLoad = type;
    console.log("CPL " + CurrentPageLoad);
}
</script>

console.log 确实显示了。

最佳答案

您可能没有添加 ID 为 C2 和 C3 的 div

试试这个 https://www.w3schools.com/code/tryit.asp?filename=G5YT4BCP4J3J

    <script>
        var CurrentPageLoad;
        function ShowFarRight(type) {
            console.log("Far Right-1");
            $(" #C3 ").empty();
            $(" #C3 ").scrollTop();
            var filename = "text_" + type + ".html";
            var filename = 'https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_table_basic&stacked=h'; //remove this line
            console.log(filename);
            $( "#C3" ).hide().load( filename ).fadeIn(500);
            console.log("Far Right-2");
        }

        function ShowCenter(type) {
            $(" #C2 ").empty();
            $(" #C2 ").scrollTop();
            var filename = "text_" + type + ".html";
            var filename = 'https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_table_basic&stacked=h'; //remove this line
            $( "#C2" ).hide().load( filename ).fadeIn(500);
            CurrentPageLoad = type;
            console.log("CPL " + CurrentPageLoad);
        }
    </script>
    <div class="C1"><button class="button_01" id="first" onclick="ShowCenter(1); ShowFarRight(150);">Button Text Here</button></div>
    <div id="C2"></div>
    <div id="C3"></div>

关于javascript - JQuery 函数被调用,未执行并且开发控制台中没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57021761/

相关文章:

javascript - 用于响应式容器填充剩余空间的 CSS

javascript - 使用 Jquery 添加内容

javascript - 使用 jQuery 将点击事件添加到类中

javascript - 将数组转换为具有默认值的对象的更简洁的方法? (Lodash 可用)

jquery - 检查用户输入的各个值是否为数字

jquery - 通过单击 textillate.js 中的单个元素来调用方法两次或多次

javascript - XMLHttpRequest 无法加载文件。仅 HTTP 支持跨源请求。 Angular.js

php - Laravel 5 访问ajax Post数据

javascript - 使用 vanilla javascript 延迟后添加类

javascript - 模糊验证的 jquery