我想用jQuery的
data()
1个
. 但是每次我这样做
undefined
从…返回
data()
.
现在,如果我定义完全相同的Javascript
在HTML中
数据()
下面是GreaseMonkey脚本:
(function(){
//boilerplate greasemonkey to wait until jQuery is defined...
function GM_wait()
{
if(typeof unsafeWindow.jQuery == 'undefined') {
window.setTimeout(GM_wait,100);
} else {
var $ = unsafeWindow.jQuery;
$(function() { letsJQuery($); });
}
}
GM_wait();
function letsJQuery($)
{
//store the data initially
$('textarea[name=comment]').data('tst', 'abc');
//retrieve the data on spacebar
$('textarea[name=comment]').live('keypress', function(e) {
if(e.which == 0x20) { //spacebar
alert("the stored data is: " + $(this).data('tst'));
return false;
}
});
}
})();
<html>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<textarea name="comment"></textarea>
</body>
</html>
1个