這是我爲編寫jQuery書籤找到的一個腳本,並且我向它添加了三行代碼。問題是jQuery代碼有很多引號(對於選擇器),因爲我必須將bookmarklet放在一個href =「javascript:code」10一切都會與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>
當我的書籤點擊鏈接,螢火說:語法錯誤:缺少}後,函數體
但如果我只運行的JavaScript(不使用HTML鏈接),它運行良好。
最簡單的解決方法是將代碼放在外部的js文件,並有書籤添加文件到頁面。 – epascarello
你確實在html代碼中有換行符,還是隻是在這裏尋找相當的因素? – epascarello
在您當前的腳本中,因爲您沒有在代碼中使用雙引號(用於分隔HTML屬性),所以書籤庫中的引號不存在問題。但是,問題是您的意見和換行符。 – MrWhite