点击使用JavaScript制作div的按钮? [英] Click a button made from div with JavaScript?

查看:155
本文介绍了点击使用JavaScript制作div的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google+中,用于发表评论的按钮来自div:

In Google+, the button that is used to post a comment is made from a 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>  

我想我可以点击它:

document.getElementById(":1vq.post").click();  

但是它说该元素没有属性点击。我发现 onclick 为空。那我怎么能用JavaScript点击按钮呢?

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?

推荐答案

编辑:与wong2聊天后谁开始了这个问题和很多对这个问题的真实含义(这个问题编写得很糟糕)的猜测失败了,他们想要做的是写一个Greasemonkey用户脚本,让Enter键按下Google+流中的发表评论按钮。由于我还没有邀请加入Google+,我甚至看不到相关代码,所以我无能为力。这个问题不是要将与Google+相关的任何内容放在您自己的网站中 - 而是要尝试使用Greasemonkey在您自己的浏览器中修改Google网站的行为。

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.

之前的尝试帮助:

id =:1vq.post不是合法的CSS ID名称。您不能在选择器名称中使用:字符。这会导致多个问题,因为它不仅不是合法字符,而且在CSS选择器语法中也是一个有意义的字符。所以,我看到你有两个问题。

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.

首先,你的选择器逻辑不起作用。其次,正如其他人所说的那样,你不能只用普通的javascript(例如没有框架)来分配这种方式。

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).

如果你改变你的选择器逻辑工作正确地说,你可以让它正常工作(使用jQuery),如下所示:

If you change your selector logic to work correctly, you can get it to work properly (using jQuery) like this:

<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.");
});

而且,你可以在这个jsFiddle中看到它的运行情况: http://jsfiddle.net/jfriend00/Yfnc7/ 。单击运行,然后单击发表评论。

And, you can see it in action in this jsFiddle: http://jsfiddle.net/jfriend00/Yfnc7/. Click Run and then click on the Post Comment.

这篇关于点击使用JavaScript制作div的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆