zl程序教程

您现在的位置是:首页 >  后端

当前栏目

ASP.NETjQuery实例10动态修改hyperlink的URL值

实例ASP 修改 10 动态 url
2023-06-13 09:14:32 时间
1.先准备界面代码:
复制代码代码如下:

<formid="form1"runat="server">
<divalign="left">
<fieldsetstyle="width:300px;height:200px;">
<tablecellpadding="0"cellspacing="0"border="0">
<tr>
<tdstyle="width:10px">
</td>
<td>
<p>
更新网址:</p>
<asp:RadioButtonListID="rblUrl"runat="server">
<asp:ListItemText="新浪"Value="http://www.sina.com.cn"></asp:ListItem>
<asp:ListItemText="百度"Value="http://www.baidu.com"></asp:ListItem>
<asp:ListItemText="网易"Value="http://www.163.com"></asp:ListItem>
</asp:RadioButtonList>
<br/>
<asp:HyperLinkID="hyperLink"runat="server">点击这儿</asp:HyperLink>
</td>
</tr>
</table>
</fieldset>
</div>
</form>

2.RadioButtonList转换成<table/>,其成员转换为<inputtype="radio"/>,下面是实现改变URL值的脚本代码:
复制代码代码如下:

<headrunat="server">
<title>Recipe10</title>
<scriptsrc="Scripts/jquery.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(document).ready(function(){
$("input[type=radio]").bind("change",function(){
$("#<%=hyperLink.ClientID%>").attr("href",$(this).val());
});
});
</script>
</head>

3.实现界面效果:

4.另外我们可以通过下面的代码实现绑定change事件:
复制代码代码如下:
$("input=[type=radio]").live("change",function(){
$("a").attr("href",$(this).val());
});

5.live()和bind()函数的区别:
live()函数可以附加事件到现在的和将来的页面元素上。然而,bind()函数只能把事件附加到已经加载过的页面元素上。
也就是说bind()适合页面静态元素而live()适合页面静态和动态元素。