c#
  • javascript
  • asp.net
  • input
  • 2012-07-31 139 views 0 likes 
    0

    我有以下JS:JavaScript的按钮点击

    <script> 
        function SetContent(link) { 
          document.getElementById('lowerContent').innerHTML = 
         "<iframe frameborder='0' src='" + link + "' width=100%' height='" + (screen.height - 100) + "px'"; 
         } 
    </script> 
    

    都会调用像这样:

    <a href="javascript:SetContent('http://urlhere);" title="URL">URL</a> 
    

    我怎样才能让<a>执行按钮点击?或者如何分配"javascript:SetContent('http://urlhere);"<input type=button>

    不需要回传。 点击只是重置主div,并添加一个iframe与提供的URL。 工作正常<a>但不知道如何将其移动到按钮。

    onclick只是导致回发,没有别的。

    回答

    2

    分配JavaScript来点击按钮的工作原理是这样的:

    <input type="button" onclick="javascript:SetContent('http://urlhere);" value="Click me at your own risk!"> 
    

    (ID等)进行相应的调整属性。

    +0

    是的就是这样。一个小问题,因为我在页面周围有一些其他的asp:按钮,当我在文本框中输入文本并点击输入时,激活了其他按钮,而不仅仅是上面的按钮。如何解决这个问题? – user1468537 2012-07-31 12:50:50

    +0

    我鼓励你提出另一个问题,提供相关的代码位(ASP和C#)。 – Alex 2012-07-31 12:53:51

    0

    试试这个。使用onclick而不是href。

    <a href="#" onclick="javascript:return SetContent('http://www.google.com');" title="URL">URL</a> 
    

    或者

    <a href="javascript:void(0);" onclick="javascript:return SetContent('http://www.google.com');" title="URL">URL</a> 
    
    2
    <input type="button" onclick="SetContent('http://urlhere');" /> 
    
    +0

    +1,修正你的错字 - onclick而不是onclik – Chibuzo 2012-07-31 12:47:59

    0

    在你设置的脚本中返回false。由于这一点,它返回true并调用回发。你不需要在href上添加javascript。你可以添加onclick如下:

    <input type="button" onclick="SetContent('http://urlhere');" /> 
    
    <script> 
        function SetContent(link) 
        { 
         document.getElementById('lowerContent').innerHTML = "<iframe frameborder='0' src='" + link + "' width=100%' height='" + (screen.height - 100) + "px'"; 
         return false; 
        } 
    </script> 
    
    相关问题