【问题标题】:Getting the active or focused element Id in a web page获取网页中的活动或焦点元素 ID
【发布时间】:2011-06-22 15:19:53
【问题描述】:

假设我的网页上有两个 iframe 和一个工具栏,用于将文本设置为粗体、下划线、斜体等操作。iframe 设置为 DesignMode,以便用户可以输入文本并将其用作文本编辑器。我试图为 iframe 使用单个工具栏....那么有没有一种方法可以让我知道用户编辑了哪个 iframe,以便可以将操作(粗体、斜体等)应用于该 iframe Id?

我想到的算法是先获取活动元素 id 然后应用操作,但我不知道这样做的语法!!

谢谢!!

【问题讨论】:

    标签: javascript html iframe


    【解决方案1】:

    好的,这就是它的答案...

    只需使用传递 id 或名称的元素注册一个 onClick 事件,并且你在被调用函数中拥有元素 id...

    <html>
    <head>
    <script type="text/javascript">
    function def()
    {
        myFrame1.document.designMode="on";
        myFrame2.document.designMode="on";
        document.getElementById("myFrame1").contentWindow.document.addEventListener('click', function(event) {clic("myFrame1");}, false);
        document.getElementById("myFrame2").contentWindow.document.addEventListener('click', function(event) {clic("myFrame2");}, false);
    }
    function clic(id) 
    {
            alert(id + " is clicked/focused");
    
    
    }
    
    </script>
    </head>
    <body onLoad="def()">
    <iframe id="myFrame1" border="0">
    </iframe>
    <iframe id="myFrame2" border="0">
    </iframe>
    
    </body></html>
    

    我很确定这也适用于任何其他 html 元素! (仔细检查一下)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-18
      • 2012-04-17
      • 2013-09-14
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多