【问题标题】:Add onkeyup attribute to div using a function使用函数将 onkeyup 属性添加到 div
【发布时间】:2021-08-10 22:11:12
【问题描述】:

我正在尝试将onkeyup="Preview.Update()" 作为属性添加到div。

由于div 已合并到 Wordpress 上的插件中,我不想编辑它。

【问题讨论】:

    标签: javascript html css wordpress


    【解决方案1】:
    document.getElementsByTagName("div").onkeyup= function() {
        Preview.Update();
    }
    

    【讨论】:

    • 非常感谢您的回复。我想应用函数的 div id 是 MathInput 所以我会写 document.getElementsByTagName("MathInput").onkeyup= function() { Preview.Update(); }
    • @Jack document.getElementById("MathInput")
    • 再次感谢。当我这样做“意外=”时,它说解析错误?有什么想法吗?
    • @Jack 可能是你的脚本在插件和脚本不知道什么是 id "MathInput" 之前运行...
    • 感谢 C.Raf.T 的帮助 :)
    【解决方案2】:

    使用jQuery

    $("#yourDivID").attr("onkeyup", "Preview.Update()");
    

    【讨论】:

    • 对不起,对 wordpress 很陌生...我只是把它放在 wordpress 主题的 functions.php 中吗?感谢您的回复。
    • 主题 HTML 端的 <script> 标记中没有。但我的示例是向元素添加属性。如果页面上有多个MathInputs,则使用listener 的C.Raf.T 示例会更容易。
    猜你喜欢
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2016-02-14
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多