doubian6241 2012-05-14 06:03
浏览 45
已采纳

使用jQuery更改动态创建的div内容

I'm getting pretty frustrated here.

I have a table of records. Next to each record will be a button. On clicking the button next to that record, a jquery AJAX call should be executed.

    <script language="JavaScript" type="text/javascript">
<!--
function swapContent(cv) {
    $("#myDiv").html('<div align="center"><img src="images/loader.gif"/></div>').show();
    var url = "process.php";
    $.post(url, {contentVar: cv} ,function(data) {
       $("#myDiv").html(data).show();
    });
}
//-->
</script>

The associated HTML:

<div id="myDiv" align="center"><a href="#" onClick="return false" onmousedown="javascript:swapContent('do_stuff');"><img src='images/icon.png' border='0'></a></div>

When I click on the HTML link, the contents of the div "myDiv" are changed out for the output of the ajax call to process.php. This is all well and good, but I will have dozens of these divs, and I need to be able to call the swapContent function with not only the parameter cv, but also pass in a parameter for which div should have its contents altered.

I believe my error is just based on an ignorance of JS syntax. For instance, I've done this:

    <script language="JavaScript" type="text/javascript">
<!--
function swapContent(thediv,cv) {
    $(thediv).html('<div align="center"><img src="images/loader.gif"/></div>').show();
    var url = "process.php";
    $.post(url, {contentVar: cv} ,function(data) {
       $(thediv).html(data).show();
    });
}
//-->
</script>

but I'm not sure if that is then treating the variable thediv as a string when it should be an object or what might be the problem.

I would appreciate any help you might give!

<div id="div1" align="center"><a href="#" onClick="return false" onmousedown="javascript:swapContent('div1','do_stuff');"><img src='images/icon.png' border='0'></a></div>
...
<div id="div2" align="center"><a href="#" onClick="return false" onmousedown="javascript:swapContent('div2','do_stuff');"><img src='images/icon.png' border='0'></a></div>
  • 写回答

4条回答 默认 最新

  • douxi3977 2012-05-14 06:09
    关注

    As you are passing the id of the div to the function so, in place of $(thediv) -> $("#"+thediv) will work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?