0
我想用jQuery製作一個用戶腳本。我希望它從光標下的元素獲取光標下的文本。問題是我寫的腳本使用了jQuery的.text(),如果元素有child,它將返回子元素的連接。任何想法如何讓文本只有我點擊的容器?如何獲取光標下的文本?
(我ebay.com試了一下,導致該網站是複雜的)
// ==UserScript==
// @name Selector
// @namespace http://userstyles.org
// @description none
// @include http://*.*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
var $;
// Add jQuery
(function(){
if (typeof unsafeWindow.jQuery == 'undefined') {
var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement,
GM_JQ = document.createElement('script');
GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
GM_JQ.type = 'text/javascript';
GM_JQ.async = true;
GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
}
GM_wait();
})();
// Check if jQuery's loaded
function GM_wait() {
if (typeof unsafeWindow.jQuery == 'undefined') {
window.setTimeout(GM_wait, 100);
} else {
$ = unsafeWindow.jQuery.noConflict(true);
letsJQuery();
}
}
function letsJQuery() {
//alert("Loaded jQuery" + $().jquery); // check jQuery version
$("*:not(html, head, body)").hover(function() {
$(this).css("border", "1px dotted blue");
},
function() {
$(this).css("border", "none");
}).click(function() {
var s = $(this).text();
if (s) alert(s);
return false;
});
}
問題在於它也會將所有子文本連接在一起。 – blez 2011-01-24 21:14:59
@blez,根據定義,任何給定元素的文本都必然包含其子元素的文本。 – eyelidlessness 2011-01-24 22:33:39