我正在尝试修改一个链接,当该链接悬停在上面时,它会显示javascript:void(0)。
$(document).ready(function(){
if( $('.loggedinusername').length ) {
$("div#moresearches ul").append("<li id='xxx'><a href='https://exmample.org'>Link</li>");
}
});我试过了:
$(document).ready(function(){
if( $('.loggedinusername').length ) {
$("div#moresearches ul").append("<li id='xxx'><a href='javascript:void(0)' onclick="location.href='https://example.org'>Link</li>");
}
});但它似乎不起作用。我注意到撇号有一些问题,但我似乎无法解决如何使它们工作。如果你需要更多的澄清,请告诉我,我很乐意提供。任何帮助都将不胜感激。
发布于 2020-05-15 20:47:55
这里有一种更简洁的方法。无论如何,这是一种完全错误的安全方法。这样把链接放到你的html中。
<li id='xxx'><a href="javasctipt:void(0)" data-url="https://exmample.org" class="no-show-url" >Link</li>然后,在准备好文档时,您只需执行以下操作:
$(document).ready(function(){
if( $('.loggedinusername').length ){
//I don't want you to see and follow the link
$(".no-show-url").click(function(){
e.preventDefault(); //not strictly necessary but reinforce the behaviour
});
}else{
//you don't see but can follow the link
var url = $(".no-show-url").data("url");
window.location(url);
}
});发布于 2020-05-15 20:53:48
您只需转义双引号,如:
onclick=\"location.href='https://example.org'\"
___^___ ___^___否则,它会弄乱生成的输出,重定向也不会起作用。
演示:
$("div#moresearches ul")
.append("<li id='xxx'><a href='javascript:void(0)' onclick=\"location.href='https://example.org'\">Link</li>");<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="moresearches">
<ul></ul>
</div>
发布于 2020-05-15 20:59:56
您可以在锚点标记上使用title属性添加工具提示,每当鼠标悬停在其上时,它都会出现。
<li id='xxx'><a title="javasctipt:void(0)" href="https://exmample.org" >Click here </li>我认为这是最优化的方式。
https://stackoverflow.com/questions/61819286
复制相似问题