【问题标题】:Image not changing on click点击后图片不变
【发布时间】:2016-10-23 02:07:27
【问题描述】:

我正在从数据库中检索图像并希望它们在点击事件时更改。

点击事件:

<script type="text/javascript">
    $(document).ready(function () {
        $('.chnj').onclick(function () {
           $('#rotatetest').toggle();
         });
    });
</script>

包含图像的 div:

<div class="col-md-12" id="rotatetest">

  <asp:Repeater ID="rptrTest" runat="server">
    <ItemTemplate>
      <div class="col-md-9 col-md-push2 test" style="margin-top:50px;">
        <img class=" image img-circle" height="200" width="200" src="<%# Eval(" PhotoPath")
           %>" style="border:10px solid white;" id="img" />
      </div>
    </ItemTemplate>
  </asp:Repeater>

  <div style="margin-top:50px;text-align:left;">
    <h3 style="color:white;font-family:Pristina;">
      <%# Eval("Description") %>
    </h3>
  </div>
</div>

当单击具有类 chnj 的 span 元素中的 glyphicon 时,应触发 onclick。

【问题讨论】:

  • 能否请您补充更多信息。
  • 您的点击方法不是有效的 jQuery 方法,请尝试使用.on().click()。并且从表面上看你的功能,一旦点击触发元素,它所要做的就是切换#rotatetest的可见性
  • 我想你会想要使用 click() 而不是 onclick:api.jquery.com/click

标签: javascript jquery html css


【解决方案1】:

jquery中没有onclick事件,应该是click

$('.chnj').click(function () {
     $('#rotatetest').toggle();
});

您也可以使用事件委托on()

$('body').on('click', '.chnj', function () {
    $('#rotatetest').toggle();
});

onclick 可以用作内联事件:

<button onclick="myFunction()">Click me</button>

或者在原版 js 中:

myElement.onclick=function(){ myScript };

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    在 Jquery 中你需要使用.click,对于多个事件你可以使用on 而不是"onclick"。因为onclick在javascript代码中使用。

    【讨论】:

    • 切换功能不会在点击事件时改变图像,而是会产生模糊效果。我需要更改 onclick 事件的图像。我尝试更改 src 属性,但这不是动态的可行选项
    • 这个链接可能对你有帮助:stackoverflow.com/questions/12355799/…
    • 如果我想在 jquery 中使用 src 属性更改图像怎么办。这段代码有什么问题?
    • $(document).ready(function () { $("#imgtwo, #imgthree,#imgfour").hide(); $(".chnj").click(function () { var src = $('#imgone').attr('src'); //chnj img1 to img2 if (src == 'Images/Support.jpg') { //chnj img2 to img3 $("#img ").attr("src", "Images/Prodbac1.jpg"); } else if (src == "Images/Prodbac1.jpg") { $("#img").attr("src", "Images /quality.jpg");} else { $("#img").attr("src", "Images/slide.jpg"); } }); });
    • 请在小提琴中发帖
    猜你喜欢
    • 2021-10-30
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 2017-05-03
    • 2014-03-22
    相关资源
    最近更新 更多