javascript
  • requirejs
  • 2017-11-05 4 views 0 likes 
    0

    どのように他のjavascriptにjavascriptファイルを含めることができますか?javascriptファイルの使用方法

    フロントエンドのJavaScriptです。

    私はHTMLに含めたくない、私はnodeJSのようにしたい:

    var jquery = require("jquery.js"); 
    jquery.$("#c").style.color = "green"; 
    

    私はこの試みた:

    const jqLink = 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js'; 
     
    /* I try this, what give me a error, with requireJS */ 
     
    define([jqLink], function(jq){ 
     
    return jq; 
     
    }); 
     
    
     
    jq$("#c").style.color = "red";
    <html><head><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script> 
     
    </head> 
     
    <body><h1 id="h1tag">Im h1tag</h1> 
     
    </body>

    PDを:私はこれを試してみましたtoo

    Main.js:

    window.addEventListener("DOMContentLoaded",main); 
    
    function main(){ 
        scripts(); 
        var e = new ES(); 
        e.get("#ti").style.opacity = +true; 
    } 
    
    function scripts(){ 
        var list = ["es.js"], script = document.createElement("script"),b = document.head, i=0,max = list.length; 
        for(;i<max;i++){ 
        script.src = list[i]; 
        b.appendChild(script); 
        } 
    } 
    

    ES.JS:

    class ES{ 
        constructor(){ 
        } 
        get(symbol, e){ 
        let d = document; 
        return symbol === "#" ? d.getElementById(e) : symbol === "." ? d.getElementsByClassName(e) : d.getElementsByTagName(e); 
        } 
    } 
    
    +1

    そんなにクライアント側 –

    +0

    に同期インポートできませんの...?ジョナスW. –

    答えて

    1

    これはうまくいくかもしれないだけで微調整です。それはあなたが同じページに

    function includeFile(path) { 
        var imported = document.createElement('script'); 
        imported.src = path; 
        document.head.appendChild(imported); 
    } 
    

    のスクリプトを「含まれている」ロードのようにそれを呼び出します。

    includeFile('/path/to/other_js_file.js'); 
    
    +0

    mmが動作しない、私はそれを試みた –

    関連する問題