问题描述
这可能吗?
我会改</div><div id="id" jq="$('#anything').css({background: 'red'})"></div>var x = '$('#id').attr('jq')';jQuery.call(x);基本上我想在任何标签上使用属性 jq
来像 onclick
在调用 javascript 语句的 html 上一样.
顺便说一句,jQuery.call() 是为了演示目的,它不起作用...
解决方案 你会使用 eval()
但你所做的似乎是一个非常糟糕的主意:
var x = $('#id').attr('jq');评估(x);
这很糟糕的原因是您没有将功能与结构分开.HTML 旨在描述结构,而不是交互性和功能.
JavaScript 旨在提供交互和增强,而不是结构.像这样使用 jQuery 内联违反了这种分离.仅供参考...
is this possible?
<div id="anything">I will change</div>
<div id="id" jq="$('#anything').css({background: 'red'})"></div>
var x = '$('#id').attr('jq')';
jQuery.call(x);
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() is for demonstration purposes it doesn't work...
解决方案 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...
这篇关于jquery 字符串到函数转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-23 19:17