开发者

creating jquery plugin - does not work when used on the same element twice

开发者 https://www.devze.com 2023-01-31 18:14 出处:网络
I\'ve been using prototypeJS until now, so forgive me for this nooby jquery question :P Take a look at this: http://jsfiddle.net/wAcVs/

I've been using prototypeJS until now, so forgive me for this nooby jquery question :P

Take a look at this: http://jsfiddle.net/wAcVs/

The plugin is being applied to #cont1 on load. #cont2 has not been touched so far. When you click on the buttons at开发者_C百科 the top, you can see that its working for #cont2, but not for #cont1.

I can see the arguments from

$('#cont1').myPlugin({id:'321', width:'200', height:'100'}) 

are taking effect on my options variable, but from then on "this" is empty, and the element is not changing at all.

How can i get this working?

Thanks in advance!


It doesn't work twice because you're changing the ID to something else, so when using $("#cont1") for example, there's nothing to find. If you just remove the .attr('id', 'plugin_'+options.id), you'll see it working.

You can test it with the changes here, note that I changed the .changed selector, since when the id remains, the #cont1 and #cont2 CSS selectors are more specific than just .changed.

0

精彩评论

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