开发者

Greasemonkey & jQuery How do I manipulate an Element I just created?

开发者 https://www.devze.com 2023-03-17 10:00 出处:网络
So I\'m trying to make a little userscript to send data to a server and react to the answer (by manipulating the link I added to the site)

So I'm trying to make a little userscript to send data to a server and react to the answer (by manipulating the link I added to the site)

jQuery('table.borderlist').after('<a href="#" id="action"><Click here to do derp</a><br />');

This works and adds the link to the site now I bind my new element to a click handler using live() (because that seems to be the only way to work with my new link (id=action)) It looks something like this:

    jQuery('#action').live('click', function() { 
    jQuery.ajax({
      type: 'GET',
      dataType: 'jsonp',
      data: 'id=666',
      jsonp: 'jsonp_callback',
      url: 'url',
      success: function (j) {
    // in here I want to change the element with id=action but nothing I tried so far seems to work
}});});

the basic idea is t开发者_开发技巧o do something like this:

jQuery('#action').replaceWith('Action successful');

problem is that this does not seem to work on elements I've just created using the Greasemonkey Script on any other element it works fine.

Appreciate any hints in the right direction


you could try this:

jQuery('table.borderlist').after(
    $('<a href="#" id="action"><Click here to do derp</a><br />').click(function(){

    })
);

but your example with .live should work as well. are you sure your id #action is unique?


When you create the element, assign it to a variable, like so:

testvar = jQuery('table.borderlist').after('<a href="#" id="action"><Click here to do derp</a><br />');

Then, you can refer to that element by its variable later:

jQuery(testvar).doStuff(foo,bar);


Ok I have soved it now the problem was that the site uses more than one table with class borderlist 2 of them not visible -.-' so that I could not see that the script actually worked ... Now I selected only the right Element and its working like a charm.

Thanks for your help anyways.

0

精彩评论

暂无评论...
验证码 换一张
取 消