我不时地在网页上看到下面的href。然而,我不明白这是要做什么或技术。有人能详细说明吗?
<a href="javascript:;"></a>
我不时地在网页上看到下面的href。然而,我不明白这是要做什么或技术。有人能详细说明吗?
<a href="javascript:;"></a>
当前回答
请参考:
<a href="Http://WWW.stackoverflow.com">Link to the website opened in different tab</a>
<a href="#MyDive">Link to the div in the page(look at the chaneged url)</a>
<a href="javascript:;">Nothing happens if there is no javaScript to render</a>
其他回答
1. Use that java script to Clear an HTML row Or Delete a row using the id set to a span and use JQuery to set a function to that span's click event.
2. Dynamically set the div html to a string variable and replace {id} with a 1 or 2 etc. cell of a larger div table and rows
<div class="table-cell">
<span id="clearRow{id}">
<a href="javascript:" style-"color:#c32029; align:right; font-size:8pt;">Clear</a>
</span>
</div>
<div class="table-cell">
<span id="deleteRow{id}">
<a href="javascript:" style-"color:#c32029; align:right; font-size:8pt;">Delete</a>
</span>
</div>
//JQuery - Clear row
$("#clearRow" + idNum).click(function(){
$("someIDOrWildcardSelector" + idNum).val("");
$("someIDOrWildcardSelector" + idNum).val("");
$("someIDOrWildcardSelector" + idNum).val("");
});
//JQuery to remove / delete an html row
$("#deleteRow" + idNum).click(function(){
//depending upon levels of parent / child use 1 to many .parent().parent().parent()
$(this).parent().remove();
});
请参考:
<a href="Http://WWW.stackoverflow.com">Link to the website opened in different tab</a>
<a href="#MyDive">Link to the div in the page(look at the chaneged url)</a>
<a href="javascript:;">Nothing happens if there is no javaScript to render</a>
它用于在href中编写js代码,而不是像onclick这样的事件监听器,并避免在href中使用# links来使标签对HTML有效。
有趣的事实
我研究了如何使用javascript:在href属性里面,得到的结果是我可以在里面写多行!
<a href="
javascript:
a = 4;
console.log(a++);
a += 2;
console.log(a++);
if(a < 6){
console.log('a is lower than 6');
}
else
console.log('a is greater than 6');
function log(s){
console.log(s);
}
log('function implementation working too');
">Click here</a>
测试在chrome版本68.0.3440.106(正式版本)(64位) 在Firefox Quantum 61.0.1(64位)中测试
这是一种让链接在点击时完全不做任何事情的方法(除非Javascript事件绑定到它)。
这是一种运行Javascript而不是跟随链接的方式:
<a href="Javascript: doStuff();">link</a>
当没有javascript要运行时(就像你的例子),它什么也不做。
<a>元素是无效的HTML,除非它有href或name属性。
如果你想让它正确地呈现为链接(如带下划线的,指针等),那么只有当它有href属性时才会这样做。
因此,这样的代码有时被用作创建链接的一种方式,但不必在href属性中提供实际的URL。开发人员显然希望链接本身不做任何事情,这是他所知道的最简单的方法。
他可能在其他地方有一些javascript事件代码,当链接被点击时触发,这将是他想要实际发生的,但他希望它看起来像一个正常的<a>标签链接。
一些开发人员使用href='#'来达到同样的目的,但是这会导致浏览器跳转到页面顶部,这可能是不需要的。而且他不能简单地让href为空,因为href= "是一个回到当前页面的链接(即它会导致页面刷新)。
有办法解决这些问题。在href中使用空的Javascript代码就是其中之一,尽管这不是最好的解决方案,但它确实有效。