javascript链接无效

时间:2012-06-14 00:26:33

标签: javascript hyperlink

这让我有点疯狂。 Javascript链接应该触发函数来填充div。但是没有工作。

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>

4 个答案:

答案 0 :(得分:5)

onclick属性值括在双引号中,因此单引号指定字符串中的字符串。

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

http://jsfiddle.net/77CKx/

答案 1 :(得分:1)

Shredder了解了这个问题的核心。你有嵌套的引号。但是,内联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;
}

答案 2 :(得分:1)

通过使用单个撇号作为函数参数,您似乎打破了onclick。

尝试

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

答案 3 :(得分: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/

相关问题