2012-06-14 50 views
0

這讓我有點瘋狂。 Javascript鏈接應該引發函數來填充div。但不工作。JavaScript鏈接不工作

JS

function showNotes(notes,id) { 
    var notes = '<form action="editnotes.php" method="post"><textarea>'+notes+'</textarea><input type="hidden" name="id" value="'+id+'"><input type="submit" name="submit" value="Save Notes"></form>'; 
    var target = 'notebox'; 
    alert(id); 
    document.getElementById(target).innerHTML = notes; 
    return false; 
} 

HTML

<a href='#' onclick='showNotes('hi there','143');'><small>Show Notes</small></a> 
<div id="notebox"></div> 
+1

你需要躲避報價,如碎紙機說。如果您使用的是Firefox,則可以使用'console errors'來查看語法錯誤。鍵入CTRL + SHIFT + J。 – Jack

+0

有時候,語法突出顯示只會讓你知道問題出在哪裏,就像這個一樣。 –

回答

5

用雙引號onclick屬性值,所以單引號指定字符串中的字符串。

onclick="showNotes('hi there','143');" 

http://jsfiddle.net/77CKx/

+0

甚至更​​好,給鏈接一個id,並設置處理程序從腳本,那麼你不會有這個問題 –

+0

改爲雙引號沒有得到它的工作... – user1260310

+2

@ user1260310它應該.. http:///jsfiddle.net/77CKx/ –

1

碎紙機到了問題的心臟。你有嵌套的引號。但是,內聯JS非常酷。用腳本來做,問題就會消失。 http://robertnyman.com/2008/11/20/why-inline-css-and-javascript-code-is-such-a-bad-thing/

HTML

<a href="#" id="shownotes"><small>Show Notes</small></a> 

JS

document.getElementById('shownotes').onclick = function() { 
    showNotes('hi there', '143'); 
    return false; 
} 
+1

你是不是指'document.getElementById('shownotes')。onclick = function()..'? – Jack

+0

@Jack謝謝,我確實錯過了 –

+0

通過內聯JS,你的意思是即使在html標籤中指定事件並不酷並且皺眉?在腳本中設置事件是更好的做法嗎?怎麼來的?如果這是一個相當的答案,我可以發佈一個關於它的問題:) –

1

好像你正在使用單撇號的函數參數打破的onclick。

嘗試

<a href='#' onclick="showNotes('hi there','143');"><small>Show Notes</small></a> 
+0

碎紙機已經說過完全一樣的東西。重複的答案 –

+0

感謝您的編輯。這是我的第一篇文章,當然我感到沮喪。 :) –

0

工作代碼:

showNotes = function (notes,id) { 
    var notes = '<form action="editnotes.php" method="post"><textarea>'+notes+'</textarea><input type="hidden" name="id" value="'+id+'"><input type="submit" name="submit" value="Save Notes"></form>'; 
    var target = 'notebox'; 
    alert(id); 
    document.getElementById(target).innerHTML = notes; 
    return false; 
}​ 


<a href='#' onclick="showNotes('hi there','143');"><small>Show Notes</small></a> 
<div id="notebox"></div>​ 

您還可以查看它在這裏工作: http://jsfiddle.net/ZMZGk/13/