使用 javascript 和 Asp.Net 生成点击警报
我有一个 Asp.Net 按钮和一个 span 标签,我有它的图像。 现在,当我单击 span 元素(即图像)时,我应该能够自动单击我的按钮并收到警报。
我可以使用如下所示的按钮单击事件中的函数来完成此操作,但我不知道单击元素时该怎么做。
CSS:
#container a span
{
display:none;
background-image:url(images/alert.png);
background-repeat:no-repeat;
width:16px;
height:16px;
position:absolute;
right:0px;
top:0px;
}
#container a:hover span
{
display:block;
}
这是我在 中的跨度:
<div class='container a'><a href='#'>
<img src='uploads/" + document.getElementById("currentDirectory").value + "/"
+ file.name + "' width='64' height='64'/>
<span onclick='document.getElementById('<%=btnAlert.ClientID%>').click();'>
</span>
</a>
</div>";
这是我简单的 Asp.Net 按钮:
<asp:Button ID="btnAlert" runat="server" Text="Button" />
如果我使用 Javascript 函数,这是代码:
Protected Sub btnAlert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlert.Click
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "Somefunction();", True)
End Sub
I have an Asp.Net button and a span tag which I have image for that.
Now when I click the span element(i.e. the image) I should be able to click my button automatically and get an alert.
I Can do it with a function as shown below on button click event but I'm not aware how to do when I click element.
CSS:
#container a span
{
display:none;
background-image:url(images/alert.png);
background-repeat:no-repeat;
width:16px;
height:16px;
position:absolute;
right:0px;
top:0px;
}
#container a:hover span
{
display:block;
}
This is my span within <a>
:
<div class='container a'><a href='#'>
<img src='uploads/" + document.getElementById("currentDirectory").value + "/"
+ file.name + "' width='64' height='64'/>
<span onclick='document.getElementById('<%=btnAlert.ClientID%>').click();'>
</span>
</a>
</div>";
This is my simple Asp.Net Button:
<asp:Button ID="btnAlert" runat="server" Text="Button" />
This is the code if I use a Javascript function:
Protected Sub btnAlert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlert.Click
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "Somefunction();", True)
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说效果很好。
你的跨度中没有内部内容,所以也许这就是它无法触发 onclick 的原因,因为它没有占用你的 dom 中的任何空间。因此,当您认为您正在点击它时,您实际上并没有点击它,因为它不占用任何空间。
尝试在跨度中添加一些内容,就像我的示例一样。
如果这不起作用,请仔细检查以确保按钮的 ID 正确。
jsFiddle
Works fine for me.
You have no inner content in your span so maybe that's why it's not able to trigger the onclick because it's not taking up any space in your dom. So when you think you're clicking on it you're actually not hitting it since it's not taking up any space.
Try adding some content in the span like my example.
If that isn't working double check to make sure your id is correct for the button.
jsFiddle