【问题标题】:Focus on iframe every time I click on specific button每次单击特定按钮时都专注于 iframe
【发布时间】:2019-11-02 16:54:30
【问题描述】:


每次点击<button class="bold" >bold</button>我都想专注于iframe
但它只能工作一次
这是我的代码:
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();
    });
});

HTML

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

【问题讨论】:

  • 为什么不用css?有焦点选择器。 W3school
  • @MTBthePRO 他们正试图将焦点应用到 iframe,而不是基于焦点对其进行风格化。
  • 它是否模糊且不再聚焦?还是只是保持专注?
  • 第一次正常,但是当我再次点击按钮时,它根本不会聚焦

标签: javascript jquery html


【解决方案1】:

也许它是适合你的解决方案。

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

还有香草js

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2023-02-23
    • 2021-11-13
    相关资源
    最近更新 更多