单击使用 JavaScript 由 div 制作的按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6558287/
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
Click a button made from div with JavaScript?
提问by wong2
In Google+, the button that is used to post a comment is made from a div:
在 Google+ 中,用于发表评论的按钮是由 div 制成的:
<div role="button" id=":1vq.post" class="d-s-r tk3N6e-e tk3N6e-e-qc" aria-disabled="false" style="-webkit-user-select: none; " tabindex="0">Post comment</div>
I think I can click it with:
我想我可以点击它:
document.getElementById(":1vq.post").click();
But it says that the element have no attribute click
. and I found that onclick
is null. So how could I click the button with JavaScript?
但它说该元素没有属性click
。我发现它onclick
是空的。那么我怎么能用 JavaScript 来点击按钮呢?
采纳答案by jfriend00
EDIT: After a chat with wong2 who started this question and a lot of failed guesses for what this question is really about (the question is quite poorly written), what they are trying to do is to write a Greasemonkey userscript to make the enter key press the Post Comment button in Google+ streams. Since I don't have an invite into Google+ yet, I can't even see the relevant code so not much I can do. This question is not about putting anything related to Google+ in your own site - it's about trying to use Greasemonkey to modify the behavior of Google's site in your own browser.
编辑:在与提出这个问题的 wong2 交谈后,对这个问题的真正含义进行了很多失败的猜测(问题写得很糟糕),他们试图做的是编写一个 Greasemonkey 用户脚本来制作回车键按 Google+ 信息流中的“发表评论”按钮。由于我还没有收到 Google+ 的邀请,我什至看不到相关代码,所以我无能为力。这个问题不是关于在您自己的网站中放置与 Google+ 相关的任何内容 - 而是关于尝试使用 Greasemonkey 在您自己的浏览器中修改 Google 网站的行为。
Earlier attempts to help:
早期的帮助尝试:
id=":1vq.post" is not a legal CSS id name. You can't use the ":" character in a selector name. This causes multiple issues because not only is it not a legal character, but it's also a meaningful character in the CSS selector syntax. So, I see that you have two issues.
id=":1vq.post" 不是合法的 CSS id 名称。您不能在选择器名称中使用“:”字符。这会导致多个问题,因为它不仅不是合法字符,而且在 CSS 选择器语法中也是一个有意义的字符。所以,我看到你有两个问题。
First, your selector logic is not working. Second, as others have said, you can't just assign to click in this way with plain javascript (e.g. no framework).
首先,您的选择器逻辑不起作用。其次,正如其他人所说,您不能使用普通的 javascript(例如,没有框架)以这种方式分配点击。
If you change your selector logic to work correctly, you can get it to work properly (using jQuery) like this:
如果您更改选择器逻辑以使其正常工作,则可以像这样使其正常工作(使用 jQuery):
<div role="button" id="aPost" class="d-s-r tk3N6e-e tk3N6e-e-qc" aria-disabled="false" style="-webkit-user-select: none; " tabindex="0">Post comment</div>
$("#aPost").click(function() {
alert("I was clicked.");
});
And, you can see it in action in this jsFiddle: http://jsfiddle.net/jfriend00/Yfnc7/. Click Run and then click on the Post Comment.
而且,您可以在这个 jsFiddle 中看到它的实际效果:http: //jsfiddle.net/jfriend00/Yfnc7/。单击运行,然后单击发表评论。
回答by katspaugh
click()
applies only to elements like input
and button
.
click()
仅适用于input
和这样的元素button
。
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-2651361
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-2651361
onclick
would appear in 1990 and not at Google. They should be using addEventListener
.
onclick
将出现在 1990 年,而不是在谷歌。他们应该使用addEventListener
.
Try to set a breakpoint and see what functionis called on click. Then call the function directly.
尝试设置断点并查看单击时调用的函数。然后直接调用函数。
To trigger a click event handler one can use createEventplus dispatchEvent.
要触发单击事件处理程序,可以使用createEvent和dispatchEvent。
See example: http://jsfiddle.net/DzVg9/
参见示例:http: //jsfiddle.net/DzVg9/
Note, that Google Plus may actually be using mousedown
or mouseup
events.
请注意,Google Plus 可能实际上正在使用mousedown
或mouseup
事件。
回答by hugomg
Are you sure you are acurately selecting the button with $(":1vq.post")
? Perhaps you need to change it to $("#:1vq.post")
or something like that (I'm not sure how JQuery handles the :
and .
characters).
您确定您正在准确地选择按钮$(":1vq.post")
吗?也许您需要将其更改为$("#:1vq.post")
或类似的内容(我不确定 JQuery 如何处理:
和.
字符)。