버튼 클릭시 자바 스크립트를 통해 태그 의 href
속성 값 을 변경하는 방법은 <a/>
무엇입니까?
<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>
한 줄의 추가 코드이지만 구조적으로 더 나은 방법을 찾습니다.
답변
href
속성 제거 :
<a id="" onclick="f1()">jhhghj</a>
링크 스타일이 중요한 경우 :
<a href="javascript:void(f1())">jhhghj</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>
답변
링크를 클릭 할 때 동적으로 변경되도록하려면 :
<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>
답변
여기에 내 의견이 있습니다. 사용자가 제출 버튼을 누를 때 텍스트 상자에서 값을 수집하여 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>`
이것에 대한 의견이 있습니까?