我想停止按鈕的提交事件,但還要將參數傳遞到由點擊觸發的函數中。這是我認爲可以工作:preventDefault併爲點擊事件監聽器傳遞參數
function NL(line, event){
event.preventDefault();
document.getElementsByClassName('new-line')[1].innerHTML=line;
}
document.getElementsByClassName('new-line')[0].addEventListener('click', function(){NL('add new line');});
這裏的HTML:
<form class='myform'>
<div class='form-block'>
<span class='line'>1</span>
<button class='new-line'>New Line</button>
<button class='new-line'>New 2nd Line</button>
</div>
</form>
我不能改變的HTML,我只能編輯JS。提前致謝!!!
啊!謝謝!!! –