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