每次单击特定按钮时,都专注于iframe




我想每次点击 iframe 时都专注于 iframe <button class="bold" >bold</button>
但它只工作一次
这是我的代码:
JavaScript

function iFrameStart() {
    iframe.document.designMode="On";
}
document.addEventListener("DOMContentLoaded",iFrameStart,false);
$(document).ready(function () {
    $(".bold").click(function () {
       iframe.document.execCommand('bold',false,null);
       document.getElementById("iframe").focus();
    });
});

网页

<div class="container">
    <h1>Editor Demo</h1>
    <div class="button-wrapper">
        <button class="bold">B</button>
    </div>
    <label><textarea name="" id="" cols="30" rows="10"></textarea></label>
    <iframe id="iframe" name="iframe" ></iframe>
</div>

也许这对你来说是一个解决方案。

$('#iframe').contents().find('html').focus();

和香草js

const frame = document.querySelector('#iframe').contentWindow;
frame.document.querySelector('body').focus();

最新更新