【问题标题】:Clear Images from external JS file从外部 JS 文件中清除图像
【发布时间】:2012-04-10 06:35:55
【问题描述】:

我正在尝试使用外部 jquery 文件清除图像,但它甚至没有显示警报。

这是我需要清除的 div 中的图像。

 $('#<%=thumbs.ClientId%>').append("<img class='LoadclickImage' align='left' style='height:48px;width:75px;' src='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "' width='75' height='50' rel='group1' href='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "' >");

这是我的外部 JS 文件:

 self.ClearAll_button.click(function (e) {
     $('#<%=thumbs.ClientId%> img').hide;
     alert('You have cleared everything!');
     e.preventDefault(); 
     }

【问题讨论】:

  • 外部是指一个单独的 .js 文件吗?
  • 请显示实际的 HTML(浏览器看到的内容),而不是您的 asp 模板。
  • 你确定你的按钮不为NULL吗?
  • @Adreas-是的,它是从外部 .js 文件调用的。

标签: javascript jquery asp.net image external


【解决方案1】:

&lt;%=thumbs.ClientId%&gt; 在外部 javascript 文件中几乎没有意义。这是一个 ASP.NET 服务器端标记,您只能在 ASPX/ASCX WebForms 中使用。

一种可能性是在您的 ASPX 页面中定义全局 javascript 变量:

<script type="text/javascript">
    var thumbsId = '<%=thumbs.ClientId%>';
</script>

然后在你单独的 js 文件中使用这个变量:

self.ClearAll_button.click(function (e) {
    $('#' + thumbsId + ' img').hide();
    alert('You have cleared everything!');
    e.preventDefault(); 
}

另一种可能性是使用类选择器而不是 id 选择器。如果您在 ASP.NET 4.0 上运行,另一种可能性是使用 predictable ids,这要归功于 ClientIDMode 设置。

【讨论】:

  • @Darin-感谢您的回复。它现在给我一个警报,但不会删除图像。
  • @Darin-您的代码中的一个小修正已解决。对于“隐藏”,它应该是 hide();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多