在原型内部运行JS/JQuery [英] Running JS/JQuery inside of prototype
问题描述
我在原型可拖动"窗口中遇到问题. 我有一个窗口,我想在窗口内启动一些jQuery/Ajax/JavaScript函数,但似乎无济于事.
I'm having a problem with Prototype Draggable windows.. I have a window and i want to launch some jQuery/Ajax/JavaScript functions inside of the window but seems like nothing works.
看一个我用简单的JavaScript弹出脚本编写的示例,当我单击div时,它不显示,我做的每个函数都发生同样的情况.
Look an example i've made with a simple JavaScript popup script and when i click the div it doesn't show, same happens with every function i do.
<script type="text/javascript">
function show_confirm() {
var r = confirm("Press a button!");
if (r == true) {
alert("You pressed OK!");
} else {
alert("You pressed Cancel!");
}
}
</script>
<a href="#" onclick="show_confirm()"><div id="delete"></div></a>
对于jQuery,我已经尝试过noconflict()
,但仍然无法正常工作.
For jQuery i already tried noconflict()
and it still doesn't work.
这是我设置窗口的方式:
Here is how i set up the window:
function openApps() {
new UI.Window({theme: "ultra_dark",
width: 1170,
height: 630,
superflousEffects: superflousEffects}).center().show().setAjaxContent('myFile.php', {
method: "GET",
onCreate: function() {
this.header.update("Applications");
this.setContent('<div class="message">Please wait...</div><div class="spinner"></div>');
}
});
}
推荐答案
Because it is setting the content by AJAX your script is being executed in the context of a callback. The function show_confirm
probably becomes a closure within onComplete
- something which you don't need to write, it is automatic.
您可能需要将其显式分配给全局变量.
You might need to explicitly assign it to a global variable.
window.show_confirm = function() {
var r = confirm("Press a button!");
if (r == true) {
alert("You pressed OK!");
} else {
alert("You pressed Cancel!");
}
}
或者,如果您发现根本没有执行所检索的<script>
元素(我认为这可能在某些域问题中发生),则可以使用iframe代替将setAjaxContent('myFile.php', ...)
替换为setUrl('myFile.php')
.这是一个折衷方案,因为我看不到如何使用加载微调器.
Alternatively if you find the retrieved <script>
elements are not being executed at all (I think this can happen with some domain issues) then you can use an iframe instead by replacing setAjaxContent('myFile.php', ...)
with setUrl('myFile.php')
. This is a compromise as I don't see how to have a loading spinner.
这篇关于在原型内部运行JS/JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!