从 ASP.NET 内容页调用 onresize

发布于 2024-07-04 16:31:57 字数 166 浏览 11 评论 0原文

我有一个 JavaScript 方法需要在我的一个页面上运行,特别是 onresize 事件。

但是,我不知道如何从我的内容页面设置该事件。 我希望我可以将它放在我的母版页上,但我不需要在使用该母版页的所有页面上调用该方法。

任何帮助,将不胜感激。

I have a JavaScript method that I need to run on one of my pages, in particular, the onresize event.

However, I don't see how I can set that event from my content page. I wish I could just put it on my master page, but I don't have the need for the method to be called on all pages that use that master page.

Any help would be appreciated.

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

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

发布评论

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

评论(3

素年丶 2024-07-11 16:31:57

我遇到了同样的问题,并遇到了这篇文章:

IE Resize Bug Revisited

上面的代码可以工作,但是 IE 存在一个问题,即当 body 标签改变形状时会触发 onresize。 该博客提供了一种效果很好的替代方法

I had the same problem and have come across this post :

IE Resize Bug Revisited

The above code works but IE has a problem where the onresize is triggered when the body tag changes shape. This blog gives an alternate method which works well

伏妖词 2024-07-11 16:31:57

将以下内容放入您的内容页面中:

<script type="text/javascript">

// here is a cross-browser compatible way of connecting 
// handlers to events, in case you don't have one
function attachEventHandler(element, eventToHandle, eventHandler) {
    if(element.attachEvent) {
       element.attachEvent(eventToHandle, eventHandler);
    } else if(element.addEventListener) {
       element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false);
    } else {
    element[eventToHandle] = eventHandler;
  }
}

attachEventHandler(window, "onresize", function() {
    // the code you want to run when the browser is resized
});

</script>

该代码应该让您了解需要做什么的基本概念。 希望您使用的库已经包含可以帮助您编写事件处理程序等的代码。

Place the following in your content page:

<script type="text/javascript">

// here is a cross-browser compatible way of connecting 
// handlers to events, in case you don't have one
function attachEventHandler(element, eventToHandle, eventHandler) {
    if(element.attachEvent) {
       element.attachEvent(eventToHandle, eventHandler);
    } else if(element.addEventListener) {
       element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false);
    } else {
    element[eventToHandle] = eventHandler;
  }
}

attachEventHandler(window, "onresize", function() {
    // the code you want to run when the browser is resized
});

</script>

That code should give you the basic idea of what you need to do. Hopefully you are using a library that already has code to help you write up event handlers and such.

简单爱 2024-07-11 16:31:57

在您的内容页面 (C#) 中使用如下代码怎么样?

Page.ClientScript.RegisterStartupScript(this.GetType(), "resizeMyPage", "window.onresize=function(){ resizeMyPage();}", true);

因此,您可以在 Javascript 中的某个位置定义一个 resizeMyPage 函数,并且每当调整浏览器大小时都会运行它!

How about use code like the following in your Content Page (C#)?

Page.ClientScript.RegisterStartupScript(this.GetType(), "resizeMyPage", "window.onresize=function(){ resizeMyPage();}", true);

Thus, you could have a resizeMyPage function defined somewhere in the Javascript and it would be run whenever the browser is resized!

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