0

我想使用 Ctrl+Enter 来提交 Google+ 回复,但是当我向提交按钮发送“点击”事件时它不起作用。这是plus.google.com的 chrome 插件。

document.onkeydown=function(e){
if(e.ctrlKey&&e.keyCode==13){
    div_id=document.activeElement.id;
    div_id=div_id.substr(0,3);
    editorid=div_id.substr(0,2)+String.fromCharCode(div_id.charCodeAt(2)-1);
    postbuttonid=editorid+'.post';
    console.log(postbuttonid);
    postbutton=document.getElementById(postbuttonid);
    evt=document.createEvent("MouseEvents"); 
    evt.initEvent("click", true, true); 
    postbutton.dispatchEvent(evt);
}

}

4

2 回答 2

1

我建议你使用 jQuery:

jQuery("#:2s.post").click();

上面的代码应该可以工作,假设正如您的评论之一所述,您要单击的按钮的 ID 为“:2s.post”。

我希望这对你有用。如果您无法直接包含 jQuery,您可以尝试以下操作:

/*--- Create a proper unsafeWindow object on browsers where it doesn't exist
    (Chrome, mainly).
    Chrome now defines unsafeWindow, but does not give it the same access to
    a page's javascript that a properly unsafe, unsafeWindow has.
    This code remedies that.
*/

if (typeof unsafeWindow === "undefined") {
    unsafeWindow    = ( function () {
        var dummyElem   = document.createElement('p');
        dummyElem.setAttribute ('onclick', 'return window;');
        return dummyElem.onclick ();
    } ) ();
    if(typeof unsafeWindow === "undefined") {
        unsafeWindow = window;
    }
}
// END PROPER unsafeWindow


var script = unsafeWindow.document.createElement("SCRIPT");
    script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js';
    script.type = 'text/javascript';
    unsafeWindow.document.getElementsByTagName("head")[0].appendChild(script);
    script.addEventListener('load', function(){ 
        jQ = unsafeWindow['jQuery'];
        $ = jQ.noConflict(true); // keep jQuery local so you don't accidentally override/overwrite any variables
        requiresjQuery($);
    }, false);

在这种情况下,您的代码实际上看起来更像:

function requiresjQuery($) { // put any code in here that requires jQuery; I actually recommend putting all of your code in here.
    $("#:2s.post").click();
}
于 2013-06-30T21:20:59.400 回答
0

尝试document.forms.yourForm.submit();

于 2012-04-26T07:19:51.500 回答