2011-05-03 24 views
-3

我有Html文檔A.html和JavaScript文件A.js,如何在HTML體內編寫javascript代碼這個SPECIFIES FUNCTION res作爲事件處理程序的onclick事件的按鈕定義在表單中?javascript功能

A.html -----------

<body> 
<form> 
<input type = "button" id="butt1" value = "Press for Results" /><br /> 
</form> 

<script type="text/javascript"> 

</script> 
</body> 
+0

我認不出你想實現..你在a.html一個按鈕,並希望它調用在定義的函數是什麼。 js什麼時候點擊? – Marty 2011-05-03 03:24:33

+0

我相信他想要一個名爲'res'的函數,就像按鈕上的'onclick'動作一樣。 – 0x60 2011-05-03 03:25:59

+0

和@sira,你不必留言。 – pavium 2011-05-03 03:26:02

回答

1

我認爲這是你的意思..

a.html

<head> 
    <script type="text/javascript" src="a.js"></script> 
</head> 
<body> 
    <form> 
     <input onclick="javascript:res();" type="button" id="butt1" value="Press for Results" /><br /> 
    </form> 
</body> 

a.js

function res() 
{ 
    alert("function logic to go here"); 
} 

如果你想在一個頁面上的所有代碼..

<head> 
    <script type="text/javascript"> 
     function res() 
     { 
      alert("function logic to go here"); 
     } 
    </script> 
</head> 
<body> 
    <form> 
     <input onclick="javascript:res();" type="button" id="butt1" value="Press for Results" /><br /> 
    </form> 
</body> 
3

這是一個非常寫得不好的問題,但我想你想要做的是非常簡單的。當你包括與

<script type="text/javascript" src="A.js"></script> 

外部腳本這是所有有以下執行。因此,如果A.js有以下幾點:

function res() { 
    ... 
} 

您可以使用指定在你的HTML,因爲這樣的:

<button onclick="res()" value="call res()"> 
0

A.html

<script type="text/javascript" src="A.js"> 
<form> 
    <input type="button" name="test" value="Click me" onclick="inform()"> 
</form> 

A.js

function inform(){ 
    alert("You have activated me by clicking the grey button! Note that the event handler is added within the event that it handles, in this case, the form button event tag") 
    } 

全部一次

<script type="text/javascript"> 
function inform(){ 
alert("You have activated me by clicking the grey button! Note that the event handler is added within the event that it handles, in this case, the form button event tag") 
} 
</script> 

<form> 
<input type="button" id="button" name="test" value="Click me" onclick="inform()"> 
</form> 

調用函數在JavaScript

if(condition in which you want onclick to b called) 
    document.getElementById('button').click(); 
+0

,但我需要onclick事件的按鈕外表單意味着在javascript – sira 2011-05-03 03:34:55

+0

@Sira如果你需要onclick事件的按鈕外,那麼爲什麼你不'使用jQuery?這對我來說會很容易。 – 2011-05-03 03:42:13

+0

@Nick你在窗體中寫了onclick事件按鈕,但是我需要它在form之外..........意味着在A.html的javascript中調用res()的A.js – sira 2011-05-03 03:44:26