为什么元素上的 load()、on()、bind() 等不可用

why is load(), on(), bind() etc on element not available

我最近从 WPF 转移到 Razor Pages(单页应用程序,而不是 MVC Razor 视图)并且我无法访问元素上的函数。

我的部分观点 (crews.cshtml)

@page
@model CrewsModel
<body>
    <div class="container">
        <div class="row>">
            <button class="btn btn-primary" id="clickyButton">refresh</button>
            <p id="timeDisplays" class="row">                
                <partial name="Controls/DateTimeDisplayControl" , model="3/1/2019" />
            </p>
        </div>        
    </div>

</body>
<script src="~/lib/jquery/dist/jquery.js" type="text/javascript"></script>
<script>
      var element = document.getElementById("timeDisplays");
      console.log(element.className);
      element.load("/DateTimeComponent?handler=DisplayTimePartial");
      console.log("Reloaded");
</script>

我可以让 element.className 写到控制台。但是,当我调用加载(或 'on',或 'bind')函数时,我得到:

TypeError: element.load is not a function

我也试过:

element.on("load","/DateTimeComponent?handler=DisplayTimePartial");

为了测试,我将函数体替换为:


            if (typeof jQuery != 'undefined')
            {
                // jQuery is loaded => print the version
                alert(jQuery.fn.jquery);
            }

确保 jQuery 已加载且版本正确。

最终目标是根据需要重新加载 'timeDisplays' div 中的部分页面视图。

我正在使用 .net core 2.2 和 jquery 3.3.1

您没有使用 jQuery api 加载方法,而是使用没有加载方法的浏览器 api 所以你可以这样做:

script>
      var element = $("#timeDisplays");
      element.load("/DateTimeComponent?handler=DisplayTimePartial");
      console.log("Reloaded");
</script>

让我知道这是否适合你。