简体   繁体   English

在局部视图中加载脚本文件

[英]Load script file in partial views

I'm having a problem with my ASP.NET MVC project. 我的ASP.NET MVC项目有问题。

In my project, the user navigate between pages with tabs, every click on tab calls in ajax request to partial view and printing the relevant HTML. 在我的项目中,用户在带有选项卡的页面之间导航,在ajax请求中每次单击选项卡调用都会部分查看并打印相关的HTML。

The problem happens when the partial view contains, for example: 当部分视图包含例如时,就会发生此问题:

<script src="bla.js" type='text/javascript'></script>

First of all, after I loaded the partial view, I can't find the script tag in the page source (or Inspect Elements in chrome). 首先,加载部分视图后,我无法在页面源(或chrome中检查元素)中找到script标签。 I don't know what the browser does with that script but the second problem, that every time it's loaded again and again. 我不知道浏览器如何处理该脚本,但第二个问题是,每次加载一次又一次。

For example, if I have this event listener in my script file: 例如,如果我的脚本文件中包含此事件侦听器:

$("#button").live("click", function() { console.log("sdf") })

this method will happen again and again due the times the user navigates to this page. 由于用户导航到此页面的次数,此方法会反复发生。 If it's the second time, the event listener will call twice. 如果是第二次,则事件侦听器将调用两次。

Try Unbinding the funtion and bind it back again... 尝试解除绑定功能,然后再次将其绑定回...

$(function(){

        $("#button").unbind('click').bind("click", function() { console.log("sdf") });

}); });

Hope That Helps. 希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM