在按钮单击事件上模拟击键的问题

问题描述 投票:0回答:1

我正在尝试使用 JS 模拟箭头键击键。我对 JS 没有经验,但我找到了一些可以实现我想要的功能的代码。问题是我无法让它使用两个按钮正常工作。

HTML:

<button id="simA_plain" style="float: right; width: 50%">Simulate arrow left, no plugin</button>
<button id="simB_plain" style="float: right; width: 50%">Simulate arrow left, no plugin</button>

JS:

$("#eventTarg").bind ("keydown keypress keyup change",  function (zEvent) {
    $("#eventLog").append ('<span>' + zEvent.type + ': ' + zEvent.which + ', </span>');
} );

$("button").click ( function (zEvent) {
    if (zEvent.target.id == "simA_plain") {
        var keyVal = 39;
        $("#eventTarg").trigger ( {
            type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
        } );
    }
} );


$("button").click ( function (zEvent) {
    if (zEvent.target.id == "simB_plain") {
        var keyValb = 37;
        $("#eventTarg").trigger ( {
            type: 'keypress', keyCode: keyValb, which: keyValb, charCode: keyValb
        } );
    }
} );

问题是,当我单击 ID 为

simB_plain
的按钮时,它会模拟右键“
39
”,而不是预期的“
37
”。对于上下文,我尝试使用它通过模拟右/左箭头笔划来推进嵌入在我的网站上的谷歌表格中的幻灯片。但是,它只会向前移动幻灯片,这意味着仍在模拟
39

您可以看到它应该按照这个修改后的jsfiddle中的预期运行:https://jsfiddle.net/c4ywmqhn/7/,但是当我在我的网站上实现它时,它仍然只模拟“

39

javascript html button
1个回答
0
投票

我找不到 JS 的解决方案,但我找到了一个使用 Google Sheets Slides 嵌入系统功能的解决方案。对于遇到同样问题的人,这就是我所做的:

<div class="responsive-google-slides">
    <iframe name="col" src="https://docs.google.com/presentation/d/e/XXXX/embed?start=true&loop=true&delayms=3000&rm=minimal" frameborder="0" width="960" height="569" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>

    <!--The following two buttons enable navigation with buttons.-->
    <a class="prev" href="https://docs.google.com/presentation/d/e/XXXX/embed?start=true&loop=true&delayms=3000&rm=minimal#slide=previous" target="col">&#10094;</a>
    <a class="next" href="https://docs.google.com/presentation/d/e/XXXX/embed?start=true&loop=true&delayms=3000&rm=minimal#slide=next" target="col">&#10095;</a>
</div>

基本上,我添加了将 iframe 链接更改为电子表格链接的按钮,并在其上附加了

#slide=previous
#slide=next

© www.soinside.com 2019 - 2024. All rights reserved.