从后面的代码调用jquery

时间:2011-07-12 09:12:43

标签: c# jquery asp.net

我需要让HyperLink闪烁(不幸的是客户要求),所以我决定使用this

有人能指出我如何从后面的代码(C#)中调用它吗?

到目前为止,我已经尝试过:

private string script1 = "<script type=\"text/javascript\" charset=\"utf-8\" src=\"javascripts/jquery.blink.min.js\"></script>";
private string script2 = "$(\".selector\").blink();";
ScriptManager.RegisterClientScriptBlock(e.Item.FindControl("hlink"), e.Item.FindControl("hlink").GetType(),
                    "BlinkingScript", script1, false);
ScriptManager.RegisterStartupScript(e.Item.FindControl("hlink"),
                                                    e.Item.FindControl("hlink").GetType(),
                                                    "BlinkingScript", script2, false);

..无济于事

1 个答案:

答案 0 :(得分:2)

试图弄乱RegisterClientScriptBlockRegisterStartupScript是痛苦的,除了一些非常偏离雷达边缘的情况外,完全没有必要。它不仅难以维护,而且难以理解。在这种情况下,简单直接的方法对你来说会更好。

在您网页的<head>区块中,添加以下内容:

<!-- Assuming that you've already got jQuery registered... -->
<script language="javascript" type="text/javascript" src="Scripts/jquery.blink.min.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        // Couple of notes here:
        // ".blinky" means that it will match anything with a CSS class of "blinky".  This can be anything, not just hyperlinks.
        // Also, I'm adding an optional object parameter to the blink() method that allows me to specify parameters that alter its behavior.
        // In this case, if I just called blink() it would only blink 3 times, but I'm overriding that to blink 5 times.
        $(".blinky").blink({blinks: 5});
    });
</script>

然后,在页面正文中,在要闪烁的超链接上添加CssClass="blinky"

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#" Text="Not Blinky Link" /><br />
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="#" Text="Not Blinky Link" /><br />
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="#" Text="Blinky Link" CssClass="blinky" /><br />
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="#" Text="Not Blinky Link" /><br />
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="#" Text="Blinky Link" CssClass="blinky" /><br />
<asp:HyperLink ID="HyperLink6" runat="server" NavigateUrl="#" Text="Blinky Link" CssClass="blinky" /><br />

您也可以使用asp:Repeater,代码隐藏等来构建您的超链接项,只要您在要闪烁的CssClass上设置CssClass即可。