简体   繁体   English

从代码后面动态调用jQuery

[英]dynamically calling jquery from code behind

Ok so I'm trying to generate a N number of divs and add them to the mark up and call the "Draggable" funtion on them from the Jquery lib. 好的,所以我试图生成N个div,并将它们添加到标记中,然后从Jquery lib中对其调用“可拖动”功能。 I've got the adding of divs to the page done, now I just need to call the function and that's where I'm having trouble. 我已经在页面上完成了div的添加,现在我只需要调用函数,这就是我遇到的麻烦。 Here is what I have so far: 这是我到目前为止的内容:

    Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFoo", "<script type='text/javascript'>myFunction($('#draggable2').draggable());</script>", false);

UPDATE: I've got a work around. 更新:我已经解决了。 It's not pretty but it works, any suggestions would be great! 它虽然不漂亮,但是可以用,任何建议都很棒!

public static class MyExtensions
{
    public static void DragAble(this HtmlGenericControl NewDiv)
    {
        NewDiv.Attributes["class"] = "ui-widget-content";
        string dragmenow = "<script>$(function () { $(\"#" + NewDiv.ClientID + "\").draggable(); });</script>";
        NewDiv.Controls.Add(new Literal() { Text = dragmenow });
    }
}   

thanks! 谢谢!

Try this code : 试试这个代码:

Removed the script blocks and added document Ready. 删除了脚本块,并添加了文档就绪。

  Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFoo", "$(function (){myFunction($('#draggable2').draggable());});", true);

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

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