2017-05-07 16 views
0

私はJavascriptを初めて使っています。私はYouTubeのチュートリアルで見つけました。私はちょうど2つの数字を合計する関数を作成するためにチュートリアルを設定し、Webブラウザで実行をクリックすると、実行するHTMLファイルを作成しました。私はとても混乱しています。私を助けてください。ありがとうございました!初心者で簡単なjavascript

function add(a,b){ 
    return a+b; 

} 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Insert title here</title> 
<script type="text/javascript" src="myjs,js"></script> 
</head> 
<body> 
<script type="text/javascript">alert(add(100,200));</script> 
</body> 
</html> 
+2

を記録していた問題は何ですか?また 'myjs、js'にカンマがあります。 –

答えて

3

1つのファイルを作成しているようです。その場合にはadd機能の必要性は

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="ISO-8859-1"> 
 
    <title>Insert title here</title> 
 
    <script type="text/javascript" src="myjs.js"></script> 
 
</head> 
 

 
<body> 
 
    <script type="text/javascript"> 
 
    function add(a, b) { 
 
     return a + b; 
 
    } 
 
    alert(add(100, 200)); 
 
    </script> 
 
</body> 
 

 
</html>
はまた、ドットで置き換えられるためにここに

<script type="text/javascript" src="myjs,js"></script> 

タイプミスコンマ(,)必要ですscriptタグ内にあることを(.)

-1

これを確認してくださいこれは動作するexamplです問題のためのeは問題です。スクリプト内の

https://jsfiddle.net/rahulsingh09/Lww11j3t/

<script> function add(a,b){ 
    return a+b; 

}</script> 
<body> 
<script type="text/javascript">alert(add(100,200));</script> 
</body> 
+0

downvoteへのコメントは高く評価されます –

0

まあ、学習上の幸運が、あなたはおそらくJavascriptをちょうど良い部分のようなテーマに関する本をチェックアウトする必要があります。また、jQueryの学習は悪い考えではありません。私はあなたの例をredidし、それをユーザ入力を受け入れて名前空間を使うように変更しました。グローバルネームスペース上で関数を宣言するだけではなく、オーバーラップするメソッドを持つより大きいシステムに入るときに始めると問題はありません。

また、javascriptコードの問題を見つける上で不可欠なクロムデバッガの使い方についても学びます。あなたはデバッガが開いていた場合は、ファイル名のmyjsに気づいているだろう、JSは、文書をロードしていない、それはおそらく404

/* Lets declare a self executing method, it auto-executes and keeps the document clean */ 
 
(function(){ 
 
    
 
    // Make a new namespace, i'm going to call it NS, but you could name it anything. This uses the OR logic to create if it doesn't exist. 
 
    window.NS = window.NS || {}; 
 
    
 
    // Declare your function in the name space 
 
    NS.add = function(a, b) { 
 
    return a+b; 
 
    }; 
 
    
 
}()); 
 

 
/* Wait till the document is ready, magic jQuery method */ 
 
$(function(){ 
 

 
    // No error prevention, this is just an example 
 

 
    // Wait till the user clicks Calculate 
 
    $('#Calculate').click(function(){ 
 
    var A = $('#A').val() - 0; // Get #A value, convert to number by subtwacting 0 
 
    var B = $('#B').val() - 0; // Get #B value, convert to number 
 
    // Call your method and set the value of #C 
 
    $('#C').val(NS.add(A, B)); 
 
    }); 
 

 
});
<!-- Use jQuery, its the best --> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div> 
 
    <!-- Declare a few fields, we like user input --> 
 
    <input type="text" id="A" value="1000"/> 
 
    <input type="text" id="B" value="337"/> 
 
    <!-- Use this field to hold input --> 
 
    <input type="text" readonly id="C"/> 
 
</div> 
 
<!-- One button to rule them all --> 
 
<button id="Calculate">Calculate</button>

関連する問題