是否可以编写 Javascript 代码来解决浏览器兼容性问题?
是否可以编写 Javascript 代码来解决浏览器兼容性问题,或者我永远不需要它,而一切都应该仅使用 CSS 来解决?
我的问题:
我在 ASP.Net 应用程序中有一个菜单,该菜单呈现一个隐藏的图像控件,假设 height=0
和 width=0
> 不会渲染任何东西。在 IE 中确实如此,但在 Chrome 中它保留了一个空白区域,而且看起来很丑。
这是生成的代码:
<img alt="Skip Navigation Links" src="/WebResource.axd?d=6z..."
width="0" height="0" style="border-width:0px;" />
我编写了以下代码来解决问题:
<script type="text/javascript">
$('img[height="0"]').css('display', 'none');
</script>
我所做的是否正确并且是解决此类问题的唯一方法?
注意:我的示例中的代码是为 ASP.Net 菜单控件生成的,我无权访问它来设置特定样式
Is it OK to write Javascript code to solve a browsers compatibility problem or I should never need that and everything should be solved using CSS only?
My problem:
I have a menu in an ASP.Net application, this menu renders a hidden image control supposing that height=0
and width=0
will not render anything. That's true in IE but it keeps an empty area in Chrome and it looks ugly.
This is the generated code:
<img alt="Skip Navigation Links" src="/WebResource.axd?d=6z..."
width="0" height="0" style="border-width:0px;" />
I wrote the following code to solve the problem:
<script type="text/javascript">
$('img[height="0"]').css('display', 'none');
</script>
Is what I did true and the only way to solve such problems?
Note: The code in my example is generated for an ASP.Net menu control and I have no access on it to set a specific style
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为在这种情况下 javascript 不是必需的,因为您可以向元素添加类或 id 并设置样式或使用:
I don't think javascript is nessessary in this case as you can either add a class or id to the element and set a style or use:
您正在使用 javascript 更改 CSS 样式属性。
为什么不简单地使用它:
style="border-width:0px; display:none;"
?[编辑]
如果您确实无法了解代码的生成方式,您可以尝试为菜单
divimg
元素添加样式代码>: <代码>.parentClass img { 显示:无; }。如果无法为这些元素定义 css 类,那就有点奇怪了。
You are using javascript to change a CSS style property.
Why not simply use it like this:
style="border-width:0px; display:none;"
?[Edit]
If you really have no access to how the code is generated, you might try adding a style for the specific
img
element withing your menudiv
:.parentClass img { display:none; }
.It's a bit strange if there is no possibility to define css classes for any of these elements.