JavaScript链接不起作用

javascript link not working

本文关键字:不起作用 链接 JavaScript      更新时间:2023-09-26

这让我有点疯狂。 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>

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

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

http://jsfiddle.net/77CKx/

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;
}

似乎您正在通过将单个撇号用于函数参数来破坏 onclick。

尝试

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

工作代码:

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/