代码之家  ›  专栏  ›  技术社区  ›  Koosh

试图在javascript中返回可单击的图像,但onclick会导致页面刷新

  •  0
  • Koosh  · 技术社区  · 3 年前

    我遇到的问题是,当我点击按钮时,图像按钮会自动刷新页面。我想做的是确保当按钮被点击时,没有刷新发生,我留在页面上,没有任何变化:

     function BuildClickableImage()
     {
        var image = "";
            image  += "<a href='' ";
            image  += "  onclick =\"Method1();  \">";  //Currently no implementation for this method, its empty
            image  += "    <img src='@Url.Content("~/Pics/Pic1.png")' />";
            image  += "</a>";
    
       return image;
     }
    

    是否仍要阻止在单击时刷新页面?

    2 回复  |  直到 3 年前
        1
  •  1
  •   Spectric amamoto    3 年前

    对于HTML5,您可以简单地省略 href 锚的属性:

    function BuildClickableImage()
     {
        var image = "";
            image  += "<a ";
            image  += "  onclick =\"Method1();  \">";  //Currently no implementation for this method, its empty
            image  += "    <img src='@Url.Content("~/Pics/Pic1.png")' />";
            image  += "</a>";
    
       return image;
     }
    
        2
  •  1
  •   ᴓᴓᴓ    3 年前

    Make a HTML link that does nothing (literally nothing)

    function BuildClickableImage()
     {
        var image = "";
            image  += "<a href='#;' ";
            image  += "  onclick =\"Method1();  \">";  //Currently no implementation for this method, its empty
            image  += "    <img src='@Url.Content("~/Pics/Pic1.png")' />";
            image  += "</a>";
    
       return image;
     }
    
    

    还是我误解了问题?