简体   繁体   English

产生功能性javascript的javascript

[英]javascript that produces functioning javascript

can you write javascript that produces/writes/etc. 你能写产生/写/等的JavaScript吗? functioning javascript? 运行的JavaScript?

for example, have a link that has a function tied to it that when clicked produces a functioning javascipt snippet? 例如,是否有一个链接具有与其相关的功能,当单击该链接时会产生功能正常的javascipt代码段? The snippet could deal with a completely other elements. 该代码段可以处理完全其他的元素。

For example 例如

Link #1(has the javascript function that produces javascript) Link #2(does absolutely nothing for now) 链接1(具有产生javascript的javascript函数)链接2(暂时完全不执行任何操作)

Click on link #1(produces javascript snipped that says "when link #2 is clicked document.write('hello')" 点击链接#1(产生的JavaScript片段摘录为“当点击链接#2时document.write('hello')”

Clicking on link #2 now produces "hello" whereas it previously did nothing. 现在,单击链接2会产生“你好”,而以前它什么也没做。 Is that possible? 那可能吗?

Yes, you can dynamically assign event handlers described in text. 是的,您可以动态分配文本中描述的事件处理程序。

However, dynamic code generation is far more difficult than it sounds unless you're just following basic patters and replacing certain variables. 但是,除非您只是遵循基本模式并替换某些变量,否则动态代码生成要比听起来困难得多。 Writing programs that write programs has long been a fascination of the computer industry, and this gave way to functional programming, which can be done in javascript . 长期以来,编写可编写程序的程序一直是计算机行业的一种迷,而这让位给了可以 javascript 完成的 功能编程

Create the input/delete keys on the onClick handler for the datepicker, you can attach date information (or other data) when the input(s) are created. 在onClick处理程序上为日期选择器创建输入/删除键,您可以在创建输入时附加日期信息(或其他数据)。 Now, you should look into $.delegate() for how to bind handlers to those inputs created. 现在,您应该查看$.delegate()以了解如何将处理程序绑定到创建的那些输入。 $.delegate can bind handlers to elements that are not created yet, so when they are created they will fire a handler. $.delegate可以将处理程序绑定到尚未创建的元素,因此在创建它们时将触发处理程序。 By storing date relevant information in the inputs via $.data() or data- attributes you will have context aware handlers for dealing with things. 通过通过$.data()data- attribute在输入中存储与日期相关的信息,您将具有上下文感知的处理程序。

If I understand your question correctly, you could do what you want with the code below. 如果我正确理解了您的问题,则可以使用下面的代码执行所需的操作。

Not sure why you'd want to do this, though. 不过,不确定为什么要这样做。

can you write javascript that produces/writes/etc. 你能写产生/写/等的JavaScript吗? functioning javascript? 运行的JavaScript?

You can do this the way I did it, or by using eval -- though, as many coders have pointed out, eval is evil! 您可以按照我的方式进行操作,也可以使用eval进行操作-不过,正如许多编码人员所指出的那样,eval是邪恶的!

<html>

<head>

<script type="text/javascript">

function initLinks(){
    document.getElementById("link1").addEventListener("click", function(){
        document.getElementById("link2").addEventListener("click", function(){
            document.write("hello");
        }, false);
    }, false);
}
</script>


</head>

<body onload="initLinks()">

<a id="link1">Link 1</a>
<a id="link2">Link 2</a>

</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM