模拟文本框单击以获得焦点

发布于 2024-10-08 02:05:23 字数 555 浏览 7 评论 0原文

我正在尝试使用以下代码在单击按钮时触发文本框上的单击事件,因为 focus() 函数在 IE8 中不起作用

function simulateClick(elm) {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
      0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var canceled = !elm.dispatchEvent(evt);
  if(canceled) {
     // A handler called preventDefault
     // uh-oh, did some XSS hack your site?
  } else {
     // None of the handlers called preventDefault
     // do stuff
  }
}

如果该元素是复选框类型而不是文本框类型,则此代码可以正常工作,有什么我可能需要的吗添加?

I am trying the following code to fire click event on a textbox while clicking on a button as the focus() function is not working in IE8

function simulateClick(elm) {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
      0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var canceled = !elm.dispatchEvent(evt);
  if(canceled) {
     // A handler called preventDefault
     // uh-oh, did some XSS hack your site?
  } else {
     // None of the handlers called preventDefault
     // do stuff
  }
}

This code works fine if the element is of type checkbox but not textbox, is there anything I might need to add?

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

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

发布评论

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

评论(1

夜巴黎 2024-10-15 02:05:23

这是焦点代码

function ResponseEnd(sender, args) {
        if (args.get_eventArgument().indexOf("Filter") != -1) {
            var grid = document.getElementById("<%= radGridEnquiries.ClientID %>");
            var label =  $(grid).children("table").children("tbody").children(".rgRow").children("td:has(span)").children("span")[0];
            if (label != null) {
                label.focus();
            }
            else {
                var noRecTemp = $(grid).children("table").children("tbody").children("tr.rgNoRecords").children("td:has(div)").children("div");
                noRecTemp.focus();
            }
        }

        if (postInProgress)
            postInProgress = false;
    }

真实的场景是我在 Telerik RadGrid 中将一些文本框设置为过滤器并且没有过滤器图标,但是当用户发布过滤器并且请求在 IE8 中完成时,过滤器文本框仍然具有焦点,阻止用户输入新的过滤器,除非再次手动单击文本

框抱歉,如果这篇文章被视为答案,但无法使用正确的缩进代码更新此问题。谢谢

This is the code for focus

function ResponseEnd(sender, args) {
        if (args.get_eventArgument().indexOf("Filter") != -1) {
            var grid = document.getElementById("<%= radGridEnquiries.ClientID %>");
            var label =  $(grid).children("table").children("tbody").children(".rgRow").children("td:has(span)").children("span")[0];
            if (label != null) {
                label.focus();
            }
            else {
                var noRecTemp = $(grid).children("table").children("tbody").children("tr.rgNoRecords").children("td:has(div)").children("div");
                noRecTemp.focus();
            }
        }

        if (postInProgress)
            postInProgress = false;
    }

The real scenario is I have some textboxes set as filters in a Telerik RadGrid and having no filter icons but when the user posts a filter and the request is finished in IE8 the filter textbox is still with focus preventing the user to input new filters unless clicks on the textbox manually again

P.S. Sorry if this post is seen as answer but couldn't update this question with proper indented code. Thanks

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