English 中文(简体)
jQuery data() function in GreaseMonkey
原标题:

I m trying to use jQuery s data() function to store and retrieve data on an element. I want to retrieve the data stored in a textarea whenever the user enters the space bar1. However, everytime I do this I get undefined back from data().

Now, if I define exactly the same Javascript in the HTML, it works as expected. Is there some "gotcha" to data() that keeps it from working in GreaseMonkey?

Here is the GreaseMonkey script:

(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;
      }
    });
  }
})();

And here is my very simple test HTML file:

<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 This is a very simplified version of my problem, of course.

问题回答

The following should work:

  function letsJQuery($)
  {
    //store the data initially
    var ta = $( textarea[name=longtext] ).data( tst ,  abc );

    //retrieve the data on spacebar
    ta.live( keypress , function(e) {
      if(e.which == 0x20) { //spacebar
        alert("the stored data is: " + ta.data( tst ));
        return false;
      }
    });
  }




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签