【发布时间】:2012-12-24 22:56:31
【问题描述】:
这是我找到的用于编写 jQuery 小书签的脚本,我已经向其中添加了三行代码。问题是 jQuery 代码有很多引号(用于选择器),因为我必须将小书签放在 href="javascript:code" 中,所以一切都被 href 的双引号弄乱了。
这是我的代码的样子,我试图以多种方式转义双引号,但没有一个起作用。有没有办法解决这个问题?
<a href="javascript:(function(){
// the minimum version of jQuery we want
var v = '1.3.2';
// check prior inclusion and version
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
var done = false;
var script = document.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/' + v + '/jquery.min.js';
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
initMyBookmarklet();
}
};
document.getElementsByTagName('head')[0].appendChild(script);
} else {
initMyBookmarklet();
}
function initMyBookmarklet() {
(window.myBookmarklet = function() {
// your JavaScript code goes here!
var loc=window.location;
$('body').append('<form id=\'IDform\' action=\'http:/pourid.3eeweb.com/read.php\' method=\'post\' ><input name=\'url\' type=\'text\' value=\''+loc+'\' /></form>');
$('#IDform').submit();
})();
}
})();">bookmarklet</a>
当我点击书签链接时,萤火虫说:函数体后的 SyntaxError: missing }
但如果我只运行 javascript(不使用 html 链接)它运行良好。
【问题讨论】:
-
最简单的解决方案是将代码放在外部 js 文件中,并让小书签将文件添加到页面中。
-
您的 html 代码中是否真的有换行符,或者它只是为了漂亮的因素而在这里?
-
在您当前的脚本中,小书签中的引号没有问题,因为您没有在代码中使用双引号(用于分隔 HTML 属性)。但是,问题在于您的 cmets 和换行符。