jQuery字符串转换为函数 [英] jquery string to function convert
问题描述
这可能吗?
<div id="anything">I will change</div>
<div id="id" jq="$('#anything').css({background: 'red'})"></div>
var x = '$('#id').attr('jq')';
jQuery.call(x);
基本上,我想在任何标记上使用属性jq
来像调用javascript语句的html上的onclick
一样.
Basically i would like use an attribute jq
on any tag to act like a onclick
would on html that calls the javascript statements.
btw jQuery.call()用于演示目的,它不起作用...
btw jQuery.call() is for demonstration purposes it doesn't work...
推荐答案
您可能会使用eval()
,但是您正在做的事情似乎是一个非常糟糕的主意:
You would use eval()
but what you are doing seems like a really bad idea:
var x = $('#id').attr('jq');
eval(x);
这很不好的原因是您没有将功能与结构分开. HTML旨在描述结构,而不是交互性和功能.
The reason this is bad, is you are not separating function from structure. HTML is meant to describe structure, not interactivity and function.
JavaScript旨在提供这种交互和增强功能,而不是结构.像这样使用jQuery内联违反了这种分隔.只是值得深思的……
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...
这篇关于jQuery字符串转换为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!