2013-10-12 71 views
0

我有inputtext類型。當我點擊它時,我想清除它。 但是我的方法是行不通的通過點擊明文輸入

<span class="input"><input type="text" id="input1" onclick="clear()"></span> 

哪裏clear()功能

function clear() { 
    alert(event.target.id); 
    event.target.value = ""; 
} 

什麼是正確的方法是什麼?

回答

1

函數名cleardoes not seem to be workingChange the function name(比如,clearMe),並嘗試(Fiddle):

<script> 
    function clearMe(x) { 
    x.value = ""; 
} 
</script> 
<span class="input"> 
    <input type="text" id="input1" onclick="clearMe(this);" value="abcd" /> 
</span> 

對於單線:

<input type="text" id="input1" onclick="this.value='';" value="abcd" /> 
1

問題是有一個功能document.clear()這實際上被稱爲而不是你的

在這種情況下,你也將看到谷歌瀏覽器的警告,因爲該功能已經過時:

document.clear()已過時。這種方法不會做任何事情。
—谷歌的Chrome V30

選擇另一個名字:(!這應該是首選的方法)

function clearA(obj) { 
    obj.value = ""; 
} 

<input type="text" id="input1" onclick="clearA(this);" /> 

或者使用真實事件偵聽器:

document.getElementById("input1").addEventListener("click", function() { 
    this.value = ""; 
};