简体   繁体   English

当用户单击jQueryUI单选按钮时,如何停止浏览器视口移至页面顶部?

[英]How can I stop the browser viewport moving to the top of the page when the user clicks on a jQueryUI radio button?

I have got some radio buttons setup like this: 我有一些这样的单选按钮设置:

<div id="typeRadios">
    <input id="note_notetype_note1" name="note[notetype]" type="radio" value="note1" /><label for="note_notetype_note1">note1</label>
    <input id="note_notetype_note2" name="note[notetype]" type="radio" value="note2" /><label for="note_notetype_note2">note2</label>
</div>

That I turn into jQueryUI buttons like this: 我变成了这样的jQueryUI按钮:

$("#typeRadios").buttonset();

This is the resulting HTML: 这是生成的HTML:

<input type="radio" value="note1" name="note[notetype]" id="note_notetype_note1" class="ui-helper-hidden-accessible">
<label for="note_notetype_note1" aria-pressed="false" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left" role="button" aria-disabled="false"><span class="ui-button-text">note1</span></label>
<input type="radio" value="note2" name="note[notetype]" id="note_notetype_note2" class="ui-helper-hidden-accessible">
<label for="note_notetype_note2" aria-pressed="false" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left" role="button" aria-disabled="false"><span class="ui-button-text">note2</span></label>

The buttons work, but whenever I click one, the browser view-port gets returned to the top of the page, the same way it happens when you click on a <a href="#">link</a> link. 这些按钮有效,但是每当我单击一个按钮时,浏览器的视口就会返回到页面顶部,就像单击<a href="#">link</a>链接时一样。

I am using jQuery 1.4.2 and jQueryUI 1.8.7. 我正在使用jQuery 1.4.2和jQueryUI 1.8.7。 How can I prevent this behaviour? 我如何防止这种行为? Thanks for reading. 谢谢阅读。

EDIT: The <a href="#">link</a> part was missing. 编辑: <a href="#">link</a>部分丢失。

I encountered this problem recently and I have actually found a solution which might help you depending on your CSS. 我最近遇到了这个问题,实际上我找到了一个解决方案,根据您的CSS,该解决方案可能会对您有所帮助。

Basically if you are hiding your radio buttons off the page using something like: 基本上,如果您使用以下方法将单选按钮隐藏在页面外:

input[type="radio"] {
  left : -10000px;
  position: absolute;
  top : -10000px;
}

Or something similar it is the top : -10000px ; 或类似的东西是top : -10000px ; that is the issue. 这就是问题。

That is, when the radio button is inevitably focused by the label click, the page is trying to bring it into view by scrolling the browser viewport to the radio button. 也就是说,当单选按钮不可避免地被标签单击聚焦时,该页面正试图通过将浏览器视口滚动到单选按钮来使其可见。 Since the minimum scrollTop value for the browser is 0 it scrolls to the top. 由于浏览器的最小scrollTop值为0因此它将滚动到顶部。

If you just remove the rule top : -10000px ; 如果只删除规则top : -10000px ; it will fix the issue. 它将解决此问题。

Hope this helps. 希望这可以帮助。

ps the reason why preventDefault and stopPropagation won't work in this instance as mentioned as a solution in other comments is that they do not stop the default behaviour of the browser to focusing of the form field when the label is clicked. ps如在其他注释中作为解决方案所提到的,preventDefault和stopPropagation在这种情况下不起作用的原因是,在单击标签时,它们不会停止浏览器的默认行为,使其集中于表单字段。

I've tried to recreate the issue, but failed. 我试图重新创建问题,但是失败了。 I've checked this in FireFox 3.6.13, Google chrome 8.0 and Internet Explorer 8. 我已经在FireFox 3.6.13,Google chrome 8.0和Internet Explorer 8中进行了检查。

Can you give some more details to recreate the issue or may be test html page? 您能否提供更多详细信息以重现问题,还是测试HTML页面?

Do you have a click event for the radio buttons. 您是否有单选按钮的click事件。 If you have one on your event handler you can stop the propagation of the event and try once. 如果事件处理程序中有一个事件,则可以停止事件的传播并尝试一次。

function clickhandler(ev){
    ev.stopPropagation();
    ev.preventDefault();
    //Your code here
}

But whatever code is given here does not suggest anything that can cause this behavior. 但是,此处给出的任何代码均未暗示任何可能导致此行为的内容。 If you have a test page please update the post with that. 如果您有测试页,请使用该页面更新帖子。

Instead of using href="#", use href="#add" or any other relevant text. 代替使用href =“#”,而使用href =“#add”或任何其他相关文本。 This will solve the jumpting to the top of the page. 这将解决跳转到页面顶部的问题。

The problem is that when you click on label of radio button, the button automatically takes focus. 问题是,当您单击单选按钮的标签时,该按钮会自动获得焦点。 Do not use position properties like top:-100000000px and left:-100000000px . 不要使用诸如top:-100000000pxleft:-100000000px类的位置属性。

I think you just need to set Visibility property of input text to Hidden. 我认为您只需要将输入文本的“可见性”属性设置为“隐藏”即可。

input[type="radio"]{visibility:hidden;}

looks like you need to handle a click, and put there return false; 看起来您需要处理点击,然后在其中return false; at the end of handler. 在处理程序的末尾。 Or may be better to prevent bubbling the event by using: 或者通过使用以下方法来防止使事件冒泡更好:

...
ev.preventDefault();
ev.stopPropagation();

where ev - is an Event object 其中ev-是事件对象

The following click handler would prevent this 以下点击处理程序可以防止这种情况

function(e){
  e.preventDefault();
  ... your code
}

I had the same problem 我有同样的问题

FF-only quick fix (referring to your code) would be 仅限FF的快速解决方案 (请参阅您的代码)是

$("div#typeRadios input[type='radio']").hide();

solution for both FF and IE FF和IE的解决方案

The culprit was a CSS rule in main stylesheet: 罪魁祸首是主样式表中的CSS规则:

input[type="radio"]{position:relative;top:3px;}

which conflicted with jQueryUI .ui-helper-hidden-accessible class 与jQueryUI .ui-helper-hidden-accessible类冲突

I had to make this rule more specific limiting it only to the "real" forms which are used for data submission 我必须使该规则更具体,仅将其限制为用于数据提交的“真实”形式

form dl.zend_form input[type="radio"]{position:relative;top:3px;}

Similar to Christos's solution, to override the stylesheet, use 与Christos的解决方案类似,要覆盖样式表,请使用

top: auto;

Found it in a Wordpress bug report: http://core.trac.wordpress.org/ticket/23684 The idea is that setting both "left" and "top" to big negatives should be unnecessary. 在Wordpress错误报告中找到了它: http : //core.trac.wordpress.org/ticket/23684这样做的想法是,不必将“ left”和“ top”都设置为大底片。

Note to the commenters discussing the click handler or the href: jquery-ui is setting handlers for its purposes, so I would rather not alter them and then have to test accessibilty, keyboard nav, etc. The style override solution seems harmless. 请注意讨论单击处理程序或href的评论员:jquery-ui出于其目的设置处理程序,因此我宁愿不更改它们,然后不得不测试可访问性,键盘导航等。样式覆盖解决方案似乎无害。

[edit 2014/02/03] Wordpress fixed this in version 3.8.1 (or earlier). [edit 2014/02/03] Wordpress在3.8.1版(或更早版本)中修复了此问题。 This fix is kinda cool, negative positioning begone! 此修复有点酷,不再是负面定位! It's replaced with clip: rect(0 0 0 0); border: 0; 它由clip: rect(0 0 0 0); border: 0;代替clip: rect(0 0 0 0); border: 0; clip: rect(0 0 0 0); border: 0; , which is apparently the jquery-ui way. ,这显然是jquery-ui的方式。 See https://core.trac.wordpress.org/changeset/26602 参见https://core.trac.wordpress.org/changeset/26602

This thing is still absolutely positioned, so leaving in my or Christo's fix should be OK. 这个东西仍然是绝对定位的,所以保留我或Christo的定位应该没问题。

The solution for me has been to set "position: relative" on 我的解决方案是将"position: relative"设置为

input[type='radio'].ui-helper-hidden-accessible

Edit: Not working on Firefox... the radio buttons become visible 编辑:在Firefox上无法使用...单选按钮变为可见

I have encountered cases where the use of the empty anchor link href="#" would cause such viewport jumps (especially if used inside an iframe in chrome). 我遇到过使用空锚链接href =“#”会导致这种视口跳跃的情况(尤其是在chrome的iframe中使用时)。 Try replacing it with href="javascript:void(0)" 尝试将其替换为href =“ javascript:void(0)”

This is not the behaviour of the jquery UI as can be seen at http://www.jsfiddle.net/gaby/3Wz3A/ 这不是jquery UI的行为,可以在http://www.jsfiddle.net/gaby/3Wz3A/中看到

It is, most likely , some other code you add to those buttons. 很可能是您添加到这些按钮的其他代码。

What happens when you click on those radio buttons ? 当您单击那些单选按钮时会发生什么? You hide and load content or something else ? 您隐藏并加载内容或其他内容吗?

If you do attach some other handler to those radio buttons, please show that code as it could very well be the culprit.. 如果确实在这些单选按钮上附加了其他处理程序,请显示该代码,因为这很可能是罪魁祸首。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想停止浏览器请求,当用户点击 UI 中的任何按钮时,比如浏览器上的停止按钮 - I want to stop the browser request, when user clicks on any button from UI, like stop button on browser 在移动视口中时,如何阻止bootstrap 4导航栏粘在页面顶部? - How can I stop the bootstrap 4 navbar from sticking to the top of the page when in mobile viewport? 当用户单击鼠标右键时,如何阻止浏览器打开对话框? - How to stop a browser from opening a dialogue box when a user clicks the right mouse button? 当用户单击按钮时,如何使用textarea弹出窗口? - How can I have a textarea popup when the user clicks a button? 用户单击按钮时如何执行JavaScript - How can i execute a javascript when a user clicks a button 如果用户单击后退浏览器按钮,我们可以导航到其他页面吗? - Can we navigate to a different page if the user clicks on the back browser button? 当用户单击其他按钮时,如何取消选择整个单选按钮组? - How do I deselect a whole group of radio buttons when a user clicks on some other button? 在Javascript中,最好是JQuery,当用户点击浏览器中的“后退”按钮时,如何在URL中添加内容? - In Javascript, preferably JQuery, how do I add something to the URL when the user clicks “back” button in the browser? 当按钮在页面初始化时隐藏时,如何让 jQueryUI 按钮图标呈现? - How can I get jQueryUI button icons to be rendered when the buttons are hidden on page initialization? 仅当用户单击返回时,如何才能将滚动设置为 top false? - How can I set scroll to top false only when user clicks back?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM