开发者

"Snippet" jQuery Plugin cannot be called twice on the same element

开发者 https://www.devze.com 2023-02-05 11:40 出处:网络
Well, I am building an online source code editor and I use Snippet, a jQuery Plugin to make the highlighting. So, for now I have a textarea and a pre element, where I copy the textarea \'s value to pr

Well, I am building an online source code editor and I use Snippet, a jQuery Plugin to make the highlighting. So, for now I have a textarea and a pre element, where I copy the textarea 's value to pre on each keyup... So I call the function .snippet(), on document.ready() and on each keyup in order to make the syntax highlighting. But I noticed that it won't work when I call it more than once on the same element. Check my code here: http://jsfiddle.net/parisk/YkNMh/. (It won't work on keyup cause I have called it once on document.ready(), if you disable the call on document ready then it 'll work only on the first keyup)... And I would really like to solve this issue. Thanks for y开发者_开发百科our help!


The snippet plugin wraps the pre element with a <div> and also adds a class to it depending on the code style you choose.

Obviously they use those to check if the element has already be snippet'ed.

You will need to undo those changes, and a simple solution in your case is to change

$('pre').snippet("c",{style:"the",showNum:true});

to

$('pre').removeClass().unwrap().snippet("c",{style:"the",showNum:true});

example at http://jsfiddle.net/gaby/Kpphx/


I got the same problem, try catch error is: TypeError: $.browser is undefined. So after some reading on JQuery it turns out $.browser is removed in JQuery 1.9 up.

To fix add this after your JQuery link (migrate plugin):

<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>

Problem solved, hope this helps someone!

0

精彩评论

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