簡體   English   中英

在mouseover javascript上加載js文件

[英]load js file on mouseover javascript

嗨,大家好,有可能在諸如mouseover或click之類的事件上加載js文件。 我嘗試加載整個js文件而不是特定功能。

本示例在button的onClick()事件上加載指定的js文件

<button onclick="myFunction()">Click me</button>

<script type="text/javascript">
   function myFunction(){

          var file = document.createElement("script");
          file.setAttribute("type", "text/javascript");
          file.setAttribute("src", "js/js_file.js");
          document.getElementsByTagName("head")[0].appendChild(file);

   }
</script>

同樣,您也可以在按鈕或任何其他HTML元素的onMouseOver()事件上加載js。

使用jQuery的示例

 <!DOCTYPE html>
    <html>
        <head>
            <title>Test</title>
            <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#myButton').mouseenter(function() {
                        $("head").append($("<script />").prop("type", "text/javascript").prop("src", "http://code.jquery.com/jquery.mobile-1.2.0.js"));
                    });
                });
            </script>
        </head>
        <body>
            <input type="button" id="myButton" value="CLICK" />
        </body>
    </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM