如何在jquery的.html()中执行jquery/javascript代码

如何在jquery的.html()中执行jquery/javascript代码,jquery,Jquery,在jQuery.html()中,我想执行以下脚本 $("#row1").html("<td id="cellId"><script>if (selectedStr) {var elem = $(\"#cellId\"); $(elem).bind(\"onRefresh\", function() {loadColor(selectedStr); storeStr(selectedStr);});$(elem).trigger(\"onRefresh\");

在jQuery.html()中,我想执行以下脚本

$("#row1").html("<td id="cellId"><script>if (selectedStr) {var elem = $(\"#cellId\");       
$(elem).bind(\"onRefresh\", function() {loadColor(selectedStr); storeStr(selectedStr);});$(elem).trigger(\"onRefresh\");) }</script><td>")
$(“#row1”).html(“if(selectedStr){var elem=$(\“#cellId\”);
$(elem).bind(\“onRefresh\”,function(){loadColor(selectedStr);storeStr(selectedStr);});$(elem).trigger(\“onRefresh\”;)})
要求:

我有一个testPage.jsp,其中包含带有单元格的表。选择特定单元格并单击“提交”按钮后,将打开一个弹出窗口。在弹出页面中,一些数据被更改,并通过ajax进行处理。结果ajax响应必须动态设置为父行。ajax响应构建了要设置的全部内容,这就是我失败的地方。 Ajax响应将是这样的。
…..
…..
内容…
…..
内容…
... 此外,“selectedStr”是testPage.jsp中的一个javascript全局变量

问题: 1.“selectedStr未定义”是浏览器错误消息。我无法获取全局变量的引用。 2.元素绑定没有发生

我尝试使用jQuery.getScript()、jQuery.globalEval、jQuery.eval。 我无法理解JQuery的{{html}}


谢谢

我需要在动态内容中定义一个隐藏的div:

<div id="afterAjax" style="display:hidden">
if (selectedStr) {
  var elem = ... etc...
</div>
更新

这里有一把小提琴,它显示了它的工作原理——不知道为什么有人把我的答案记下来了(我在需要的时候就用这个

小提琴中的代码只是:

$('body').append('<div id="test">test</div><div id="afterAjax" style="display:none">alert("works");</div>');

eval($("#afterAjax").html());
$('body').append('testalert(“works”););
eval($(“#afterAjax”).html();

您应该以这种方式构建html:

var newRow = $('<tr/>').bind('onRefresh',function(){

{code}

});

$("#row1").append(newRow);

但是我真的不明白它应该如何在您的页面上工作,而不是将所有内容都放到脚本中,在内存中构建元素,然后在添加到DOM之前向其添加事件处理程序

$td = $("<td></td>").attr("id", "cellId")
    .bind("onRefresh", function() {
        if (selectedStr) {
            loadColor(selectedStr);
            storeStr(selectedStr);
        }
    })
    .trigger("onRefresh");

$("#row1").html($td);
$td=$(“”).attr(“id”,“cellId”)
.bind(“onRefresh”,函数(){
if(selectedStr){
loadColor(selectedStr);
storeStr(selectedStr);
}
})
.触发(“重新刷新”);
$(“#行1”).html($td);

另外,要小心添加多个具有相同Id的
td
元素。

如果您希望根据javascript输出填充html,则应使用函数作为jQuery.html()的参数。请参见下文

$("#row1").html(function (index, oldHTML){
var elem = $(this);
//This is where you could run your if and other functions
//formulate the new html 
//then just return it to set the html
return newHTML;
});

另外,更多的例子请阅读

我认为你应该去掉句子中的if,完成所有需要的步骤,最后用所需的html代码填充第1行。html中的引号转义错误……
“cellId
”“cellId”escape是问题中的一个输入错误。我正在将元素数据作为ajax响应的一部分进行构建。之后,我将其添加到DOM中。因此,您在.html()中看到的所有内容都是动态构建的。很好,只需将上面的代码添加到成功处理程序。实际上,我正在将事件绑定到。因此,对于每个事件,我都需要绑定事件。基于“selectedStr”我将决定是否绑定事件。感谢您的回复。但是一旦ajax响应内容添加到DOM中,eval()就不起作用了。eval()应该在$(“#row1”).html(ajaxRespContent);eval($(“#afterAjax”).html()中使用吗?这不起作用只是尝试一下,效果很好。是的,eval()在加载响应后,必须在脚本中。
$td = $("<td></td>").attr("id", "cellId")
    .bind("onRefresh", function() {
        if (selectedStr) {
            loadColor(selectedStr);
            storeStr(selectedStr);
        }
    })
    .trigger("onRefresh");

$("#row1").html($td);
$("#row1").html(function (index, oldHTML){
var elem = $(this);
//This is where you could run your if and other functions
//formulate the new html 
//then just return it to set the html
return newHTML;
});