Javascript:Onclick 事件在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7972458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:54:27  来源:igfitidea点击:

Javascript: Onclick event not working in IE

javascripthtmlinternet-explorerfirefoxonclick

提问by Guy TechnoBear Curtis

I have tried to google this to no avail and have tried multiple options. All of which work in firefox but none have worked in IE. I have 2 onclick events on one page and neither are working in IE. The javascript file for both events is called by:

我试图谷歌这个无济于事,并尝试了多种选择。所有这些都在 Firefox 中工作,但没有一个在 IE 中工作过。我在一页上有 2 个 onclick 事件,但都没有在 IE 中工作。这两个事件的 javascript 文件被调用:

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

The first event is an anchor event which I started with as:

第一个事件是一个锚事件,我从它开始:

<a href="" onclick="streamwindow()">Listen Live</a>

then tried

然后尝试

<a href="javascript:streamwindow()">Listen Live</a>

then:

然后:

<a href="javascript:void(0)" onclick="streamwindow()">Listen Live</a>

then:

然后:

<a href="javascript:openwindow.js" onclick="streamwindow()">Listen Live</a>

then:

然后:

<a onclick="javascript:streamwindow()">Listen Live</a>

I think that is everything I have tried so far for that one. The other click event is called when the user clicks on an image. Again works fine in firefox as is but not in IE. Code for the second event is:

我认为这就是我迄今为止为那个尝试过的一切。当用户单击图像时调用另一个单击事件。再次在 Firefox 中正常工作,但在 IE 中则不然。第二个事件的代码是:

<td width="450px" align="center">
<p>Clip of The Day</p>
<img id="image2" src="images/sound.jpg" onclick="clipwindow()" />
</td>

I have played with this one quite as much. The javascript file (openwindow.js) contains:

我玩过这个。javascript 文件 (openwindow.js) 包含:

function clipwindow()
{
window.open ('clipoftheday.html', 'Clip of the Day', 'height=200',
'width=100', 'toolbar=no', 'menubar=no', 'scrollbars=no', 'resizable=no',
'location=no', 'directories=no', 'status=no')
}

function streamwindow()
{
window.open ('stream/index.html', 'Live Stream', 'height=70', 'width=500', 'toolbar=no', 'menubar=no', 'scroolbars=no', 'resizable=no', 'location=no', 'directories=no', 'status=no')
}

Please help me with this. I look forward to hearing back from people.

请帮我解决一下这个。我期待着人们的回音。

回答by nnnnnn

The window.open()method expects all of those height, width, etc., parameters as a single string:

window.open()方法将所有这些高度、宽度等参数作为单个字符串:

window.open('stream/index.html', 'LiveStream',
            'height=70,width=500,toolbar=no,menubar=no,scroolbars=no,resizable=no,location=no,directories=no,status=no');

According to MDNthe window name and the string with the features should not contain blank space. I don't know why your existing code works in FF.

根据MDN,窗口名称和具有特征的字符串不应包含空格。我不知道为什么您现有的代码在 FF 中有效。

Most or all of your <a>variations should work, but of the ones you tried I'd suggest this one:

您的大部分或所有<a>变体都应该有效,但是在您尝试过的变体中,我建议使用以下变体:

<a href="javascript:void(0)" onclick="streamwindow()">Listen Live</a>

Better, though, would be to set the hrefto the same URL as what the JS will open, so that the link will still work for users with JS disabled. For users with JS enabled you then return false from the onclickso that the default navigation doesn't occur as well as your onclick:

不过,更好的是将 设置href为与 JS 将打开的 URL 相同的 URL,以便该链接仍然适用于禁用 JS 的用户。对于启用了 JS 的用户,您然后从 返回 falseonclick以便默认导航不会发生以及您的 onclick:

<a href="stream/index.html" onclick="streamwindow(); return false;">Listen Live</a>

If the above still doesn't work I'd suggest you temporarily replace the contents of your functions with a simple alert("In function x");message to test if the functions are being called at all.

如果以上仍然不起作用,我建议您暂时用一条简单的alert("In function x");消息替换函数的内容,以测试是否正在调用函数。

回答by Pointy

Two problems:

两个问题:

  1. The window name (for IE) mustbe a valid identifier, so "Live Stream" won't work. Try "Live_Stream" or "LiveStream" instead.

  2. The window option parameters ("scrollable", "height", etc) need to all be combined into one string, separated by commas.

  1. 窗口名称(对于 IE)必须是有效标识符,因此“实时流”将不起作用。试试“Live_Stream”或“LiveStream”。

  2. 窗口选项参数(“scrollable”、“height”等)需要全部组合成一个字符串,用逗号分隔。