Bookmarklet實現啟動jQuery(模仿 云輸入法)
更新時間:2010年09月15日 12:24:05 作者:
最近流行的 sogo云輸入法, QQ云輸入法,都用到了bookmarklet技術。
書簽本來是用來收藏 網址(http://momo.site.com/), 但實際上書簽還可以收藏 javascript代碼
只要把書簽中的地址url, 換成javascript代碼就可以了。
javascript:your_javascript_expression
上面的 javascript : 可以認為是javascript協議, 就像http: 是http協議一樣。
下面是 啟動jQuery 書簽的地址:
javascript:(
function(jquery_node){
var disable = function(src_node) {
src_node.parentNode.removeChild(src_node);
jQuery.noConflict(1);
};
var enable = function(d,j){
j=d.createElement('script');
j.id='jquery_src_code';
j.src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
d.getElementsByTagName('head')[0].appendChild(j);
};
!!jquery_node ? disable(jquery_node)
: enable(document);
})(document.getElementById('jquery_src_code'))
為了在網頁上可以讓用戶收藏,你需要把它放入一個<a>鏈接中。
比如:
<a href="javascript:your_js_code" title="啟動jQuery"><span>啟動jQuery</span></a>
全部的代碼是:
<a class="how-to-install" id="bookmarklet" title="啟動jQuery"
href="
javascript:(
function(jquery_node){
var disable = function(src_node) {
src_node.parentNode.removeChild(src_node);
jQuery.noConflict(1);
};
var enable = function(d,j){
j=d.createElement('script');
j.id='jquery_src_code';
j.src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
d.getElementsByTagName('head')[0].appendChild(j);
};
!!jquery_node ? disable(jquery_node)
: enable(document);
})(document.getElementById('jquery_src_code'))">
<span>啟動jQuery</span> </a>
結果如下:
啟動jQuery
你可以收藏上面的書簽了 (右鍵,點擊 "將此鏈接加為書簽");
FireBug---》 Console ,試試 jQuery的魅力吧.
jQuery('a');
$('a') 可能會失效,因為可能一開始就有 window.$對象(包含了別的js庫,或網站自己定義了$對象)
cnblogs 的網站就使用jQuery庫。
只要把書簽中的地址url, 換成javascript代碼就可以了。
復制代碼 代碼如下:
javascript:your_javascript_expression
上面的 javascript : 可以認為是javascript協議, 就像http: 是http協議一樣。
下面是 啟動jQuery 書簽的地址:
復制代碼 代碼如下:
javascript:(
function(jquery_node){
var disable = function(src_node) {
src_node.parentNode.removeChild(src_node);
jQuery.noConflict(1);
};
var enable = function(d,j){
j=d.createElement('script');
j.id='jquery_src_code';
j.src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
d.getElementsByTagName('head')[0].appendChild(j);
};
!!jquery_node ? disable(jquery_node)
: enable(document);
})(document.getElementById('jquery_src_code'))
為了在網頁上可以讓用戶收藏,你需要把它放入一個<a>鏈接中。
比如:
復制代碼 代碼如下:
<a href="javascript:your_js_code" title="啟動jQuery"><span>啟動jQuery</span></a>
全部的代碼是:
復制代碼 代碼如下:
<a class="how-to-install" id="bookmarklet" title="啟動jQuery"
href="
javascript:(
function(jquery_node){
var disable = function(src_node) {
src_node.parentNode.removeChild(src_node);
jQuery.noConflict(1);
};
var enable = function(d,j){
j=d.createElement('script');
j.id='jquery_src_code';
j.src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
d.getElementsByTagName('head')[0].appendChild(j);
};
!!jquery_node ? disable(jquery_node)
: enable(document);
})(document.getElementById('jquery_src_code'))">
<span>啟動jQuery</span> </a>
結果如下:
啟動jQuery
你可以收藏上面的書簽了 (右鍵,點擊 "將此鏈接加為書簽");
FireBug---》 Console ,試試 jQuery的魅力吧.
jQuery('a');
$('a') 可能會失效,因為可能一開始就有 window.$對象(包含了別的js庫,或網站自己定義了$對象)
cnblogs 的網站就使用jQuery庫。

