jquery string to function convert

后端 未结 2 935
孤街浪徒
孤街浪徒 2021-01-22 21:38

is this possible?

I will change
var
相关标签:
2条回答
  • 2021-01-22 21:59

    You would use eval() but what you are doing seems like a really bad idea:

    var x = $('#id').attr('jq');
    eval(x);
    

    The reason this is bad, is you are not separating function from structure. HTML is meant to describe structure, not interactivity and function.

    JavaScript is meant to provide that interaction and enhancement, but not structure. Using the jQuery inline like that is a violation of that separation. Just food for thought...

    0 讨论(0)
  • 2021-01-22 22:10

    you can use new Function MDN

    body:

    <div id='MyFunc' func="alert('Hello!');" >On Load raise an Alert</div>
    

    js:

    var myFunc = myFunc || document.getElementById('MyFunc'),
            hello = new Function( myFunc.getAttribute('func'));
        hello.call(this);
    

    give it a try:

    http://jsfiddle.net/msLsy/

    Hope it helps!

    0 讨论(0)
提交回复
热议问题