質問

変更する方法 href andの属性値 <a/> ボタン]クリックでJavaScriptをタグ付けしますか?

<script type="text/javascript">
  function f1()
  {
    document.getElementById("abc").href="xyz.php"; 
  }
</script>

<a href="" id="abc">jhg</a>
<a href="" id="" onclick="f1()">jhhghj</a>
役に立ちましたか?

解決

持っていない href, 、クリックが現在のページをリロードするので、次のようなものが必要です。

<a href="#" onclick="f1()">jhhghj</a>

または、このようなスクロールを防止します。

<a href="#" onclick="f1(); return false;">jhhghj</a>

または return false あなたの中で f1 機能と:

<a href="#" onclick="return f1();">jhhghj</a>

....または、目立たない方法:

<a href="#" id="abc">jhg</a>
<a href="#" id="myLink">jhhghj</a>

<script type="text/javascript">
  document.getElementById("myLink").onclick = function() {
    document.getElementById("abc").href="xyz.php"; 
    return false;
  };
</script>

他のヒント

Nick Carverがそこで行ったことはまさにそうでしたが、DOM SetAttributeメソッドを使用すると最善だと思います。

<script type="text/javascript">
   document.getElementById("myLink").onclick = function() {
   var link = document.getElementById("abc");
   link.setAttribute("href", "xyz.php");
   return false;
   }
</script>

それはコードの1つの追加の行ですが、構造的にはより良いと感じています。

削除する href 属性:

<a id="" onclick="f1()">jhhghj</a>

リンクスタイルが重要な場合は:

<a href="javascript:void(f1())">jhhghj</a>

クリック時にリンクを動的に変更するには:

<input type="text" id="emailOfBookCustomer" style="direction:RTL;"></input>
        <a 
         onclick="this.href='<%= request.getContextPath() %>/Jahanpay/forwardTo.jsp?handle=<%= handle %>&Email=' + document.getElementById('emailOfBookCustomer').value;" href=''>
    A dynamic link 
            </a>
<a href="#" id="a" onclick="ChangeHref()">1.Change 2.Go</a>

<script>
function ChangeHref(){
document.getElementById("a").setAttribute("onclick", "location.href='http://religiasatanista.ro'");
}
</script>

これが私の見解です。ユーザーが送信ボタンを押したときに、テキストボックスから値を収集してURLを作成する必要がありました。

<html>
<body>

Hi everyone

<p id="result"></p>

<textarea cols="40" id="SearchText" rows="2"></textarea>

<button onclick="myFunction()" type="button">Submit!</button>

<script>
function myFunction() {
    var result = document.getElementById("SearchText").value;
	document.getElementById("result").innerHTML = result;
	document.getElementById("abc").href="http://arindam31.pythonanywhere.com/hello/" + result;
}		
</script>


<a href="#" id="abc">abc</a>

</body>
<html>

私はその少し古い投稿を知っています。それでも、それは誰かを助けるかもしれません。

タグの代わりに、可能であれば、これもできます。

 <script type="text/javascript">
        function IsItWorking() {
          // Do your stuff here ...
            alert("YES, It Works...!!!");
        }
    </script>   

    `<asp:HyperLinkID="Link1"NavigateUrl="javascript:IsItWorking();"`            `runat="server">IsItWorking?</asp:HyperLink>`

これに関するコメントはありますか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top