删除、替换或禁用动态生成的 ASP.Net js 代码

发布于 2024-09-10 04:12:36 字数 371 浏览 5 评论 0原文

我正在使用一些 .Net 4.0 Webforms 控件,例如菜单控件,虽然我认为现在可以声明控件的呈现方式(即作为表格或 div)很棒,但我无法关闭自动包含的 javascript 管理这些控件的悬停事件,例如:

new Sys.WebForms.Menu({ element: 'NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false }

它出现在拥有此类控件的每个页面的底部。

鉴于没有办法真正关闭它,禁用该脚本、覆盖它或以其他方式使其无效以便我可以在其位置定义自己的 jQuery 方法的最佳方法是什么?

I am working with a few .Net 4.0 webforms controls such as the Menu control and while I think it's great that I can now declare the way in which controls are rendered (i.e. as either tables or divs), I can't switch off the automagically-included javascript that manages the hover events for those controls, for example:

new Sys.WebForms.Menu({ element: 'NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false }

This appears at the bottom of every page that owns such a control.

Given there is no way to actually switch this off, what is the best approach to either disabling this script, overriding it or in some other way rendering it impotent so that I can define my own jQuery methods in its place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-09-17 04:12:36

好吧,您可以尝试在包含 Webforms 脚本之后包含一些 javascript,该脚本执行以下操作:

Sys.WebForms.Menu = function() {};

基本上,它会覆盖 Webforms 菜单 javascript 以不执行任何操作。但要小心,特别是如果您正在处理已经使用这些菜单的现有项目,因为它们是 javascript 将不再工作。

就我个人而言,我建议使用“更精简”的控件(例如 Repeater 或 ListView)创建自己的菜单标记、CSS 和 javascript。这可以避免大多数问题,并且您可以更好地控制输出。

Well, what you can try is include some javascript after the Webforms scripts are included that does the following:

Sys.WebForms.Menu = function() {};

Basically, that overrides the webforms Menu javascript to do nothing. Be careful though, especially if your working on an existing project that uses these menus already, as they're javascript will no longer work.

Personally, I would recommend creating your own menu markup, css and javascript using a much "leaner" control like the Repeater or ListView. This would avoid most of these issues and you would have much more control over the output.

老街孤人 2024-09-17 04:12:36

我们的应用程序中的遗留代码刚刚遇到(或刚刚注意到)类似的问题。

与 Phil.Wheeler 一样,也使用 Sitemap 数据源。不确定将渲染模式更改为 3.5 对我们来说是一件好事,并且覆盖 Sys.WebForms.Menu 的脚本 hack 不起作用。

问题:

此代码会自动插入到每个 aspx 页面上:

<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'ctl00_MainNavMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>

我们的页面都没有 id 为“ct100_MainNavMenu”的元素,因此,我们在 MenuStandards.js 中看到一个 javascript 错误,解析 tagName === 'DIV'。 this.element 为空。

Sys.WebForms.Menu = function(options) {
    this.items = [];
    this.depth = options.depth || 1;
    this.parentMenuItem = options.parentMenuItem;
    this.element = Sys.WebForms.Menu._domHelper.getElement(options.element);
    if (this.element.tagName === 'DIV') {
        var containerElement = this.element;
        this.element = Sys.WebForms.Menu._domHelper.firstChild(containerElement);
        this.element.tabIndex = options.tabIndex || 0;
        options.element = containerElement;
        options.menu = this;
        this.container = new Sys.WebForms._MenuContainer(options);
        Sys.WebForms.Menu._domHelper.setFloat(this.element, this.container.rightToLeft ? "right" : "left");
    }
    else {
        this.container = options.container;
        this.keyMap = options.keyMap;
    }

将以下内容添加到我们的 ASPX 主文件中,因为标记之前的最后一个 html 似乎可以工作(它消除了问题):

<div id="ctl00_MainNavMenu" style="display:none">
    <div id="neededToPreventSecondErrorAt_tabIndex"></div>
</div>

呈现的 HTML 如下所示:

<div id="ctl00_MainNavMenu" style="display: none; float: left;">
    <div tabindex="0" role="menubar" class="static" style="position: relative; width: auto; float: left;"></div>
</div>

在 IE 上测试时,在我们的任何 ASPX 页面上没有看到任何不良影响、FF 和 Chrome。显然,如果在页面上创建具有相同 ID 的元素,就会出现问题。除非我们重做应用程序菜单,否则不确定这种可能性有多大。我不认为这比覆盖 webforms 菜单 javascript 更糟糕/更有风险。

Just ran into (or just noticed) similar problem with the legacy code in our app.

Like Phil.Wheeler, also using a Sitemap data source. Not sure that changing the rendering mode to 3.5 is a good thing for us and the script hack to override Sys.WebForms.Menu did not work.

Problem:

This code is inserted auto-magically on every aspx page:

<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'ctl00_MainNavMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>

None of our pages has an element with id of 'ct100_MainNavMenu' so, we are seeing a javascript error in MenuStandards.js resolving tagName === 'DIV'. this.element is null.

Sys.WebForms.Menu = function(options) {
    this.items = [];
    this.depth = options.depth || 1;
    this.parentMenuItem = options.parentMenuItem;
    this.element = Sys.WebForms.Menu._domHelper.getElement(options.element);
    if (this.element.tagName === 'DIV') {
        var containerElement = this.element;
        this.element = Sys.WebForms.Menu._domHelper.firstChild(containerElement);
        this.element.tabIndex = options.tabIndex || 0;
        options.element = containerElement;
        options.menu = this;
        this.container = new Sys.WebForms._MenuContainer(options);
        Sys.WebForms.Menu._domHelper.setFloat(this.element, this.container.rightToLeft ? "right" : "left");
    }
    else {
        this.container = options.container;
        this.keyMap = options.keyMap;
    }

Adding the following to our ASPX master file, as the last html before the tag seems to work (it gets rid of the problem):

<div id="ctl00_MainNavMenu" style="display:none">
    <div id="neededToPreventSecondErrorAt_tabIndex"></div>
</div>

The rendered HTML looks like this:

<div id="ctl00_MainNavMenu" style="display: none; float: left;">
    <div tabindex="0" role="menubar" class="static" style="position: relative; width: auto; float: left;"></div>
</div>

Not seeing any ill effects on any of our ASPX pages, testing on IE, FF, and Chrome. Clearly, there'll be a problem if an element is ever created on the page with the same ID. Not sure how likely that is unless we redo our app menus. I don't think it's any worse/riskier than overriding the webforms Menu javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文