Javascript onclick="location.href='link.html'" 不会在 Safari 中加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6418634/
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-08-23 21:39:24  来源:igfitidea点击:

onclick="location.href='link.html'" does not load page in Safari

javascripthtmlsafarionclick

提问by Brandon Lebedev

I cannot get onclick="location.href='link.html'"to load a new page in Safari (5.0.4).

我无法onclick="location.href='link.html'"在 Safari (5.0.4) 中加载新页面。

I am building a drop-down navigation menu using the <select>and <option>HTML tags. I am using the onclickhandler to load a new page after a menu-item is clicked, but nothing happens in Safari. (I have successfully tested in FF & Opera.) I know that there are many onclickbugs in Safari, but I haven't found any solutions that address this specific issue.

我正在使用<select><option>HTML 标签构建一个下拉导航菜单。onclick单击菜单项后,我正在使用处理程序加载新页面,但在 Safari 中没有任何反应。(我已在 FF 和 Opera 中成功测试。)我知道onclickSafari中存在许多错误,但我还没有找到解决此特定问题的任何解决方案。

You can see a sample of my code below:

您可以在下面看到我的代码示例:

<select>
    <option onclick="location.href='unit_01.htm'">Unit 1</option>
</select>

and

<select>
    <option onclick="location.href='#5.2'">Bookmark 2</option>
</select>

I do not (and prefer not) to have any javascript embedded in the head section of my HTML. I am developing the page for someone who does not know how to use javascript--so the simpler the code, the better.)


What JavaScript code would make the menu-item clickable in all-browsers? (Please verify compatibility with IE.)

我没有(也不喜欢)在我的 HTML 的 head 部分嵌入任何 javascript。我正在为不知道如何使用 javascript 的人开发页面——所以代码越简单越好。)


什么样的 JavaScript 代码可以使菜单项在所有浏览器中都可点击?(请验证与 IE 的兼容性。)

采纳答案by Matthew Abbott

Use jQuery....I know you say you're trying to teach someone javascript, but teach him a cleaner technique... for instance, I could:

使用 jQuery....我知道你说你想教某人 javascript,但教他一个更简洁的技术...例如,我可以:

<select id="navigation">
    <option value="unit_01.htm">Unit 1</option>
    <option value="#5.2">Bookmark 2</option>
</select>

And with a little jQuery, you could do:

用一点 jQuery,你可以做到:

$("#navigation").change(function()
{
    document.location.href = $(this).val();
});

Unobtrusive, and with clean separation of logic and UI.

不显眼,逻辑和 UI 清晰分离。

回答by Rafael Zottesso

Try this:

尝试这个:

onclick="javascript:location.href='http://www.uol.com.br/'"

Worked fine for me in Firefox, Chrome and IE (wow!!)

在 Firefox、Chrome 和 IE 中对我来说效果很好(哇!!)

回答by Richard Hedges

Give this a go:

试一试:

<option onclick="parent.location='#5.2'">Bookmark 2</option>

回答by garatu

You can try this:

你可以试试这个:

 <a href="link.html">
       <input type="button" value="Visit Page" />
    </a>

This will create button inside a link and it works on any browser

这将在链接内创建按钮,它适用于任何浏览器

回答by Jacob NP

I had the same error. Make sure you don't have any <input>, <select>, etc. name="location".

我有同样的错误。确保你没有任何<input><select>等等name="location"

回答by Kamil Kie?czewski

try

尝试

<select onchange="location=this.value">
    <option value="unit_01.htm">Unit 1</option>
    <option value="#5.2" selected >Bookmark 2</option>
</select>