显示不需要href属性的锚点的首选方法是什么(因为它仅用于javascript)?
例如,<a href="#">example 1</a>vs <a href="javascript:;">example 2</a>.
您应始终为没有Javascript的用户设置后备链接.调用javascript时,您可以从处理程序返回false以避免链接激活.例如:
<a href="page.html" onclick="doSomething(); return false;">example 1</a>
Run Code Online (Sandbox Code Playgroud)
如果没有适当的链接页面,则替代方法是根本不使用链接.请改用按钮.如果真的有必要,你可以将它设计成你的链接.
正如其他用户所提到的,如果可以使用非 javascript 后备,则可以将其用作您的 href。
使用 javascript 在弹出窗口中为用户打开图像,同时为不使用 javascript 的用户回退到常规页面加载(使用 jQuery)的示例:
<a class="popup" href="path/to/image.png">Baby Llama Picture</a>
<script>
// Assuming a function called popUpImage does the magic
$('.popup').click(function(event) {
popUpImage($(this).attr('href'));
event.preventDefault();
});
</script>
Run Code Online (Sandbox Code Playgroud)
任何没有 javascript 就没有有用的定义行为的链接都应该使用 javascript 注入到页面中。这样,没有 JavaScript 的用户将永远不会看到它们,并且无需提供有效的 href。
动态插入仅 javascript 链接的示例(使用 jQuery):
<img class="editable" src="baby-llama.png" />
<script>
// Assuming a function doEditing that allows javascript based editing of an image
$(function() {
$('.editable').each(function() {
var editable = $(this);
var editLink = $('<a href="">Edit image</a>');
editLink.click(function() {
doEditing(editable);
});
editable.insertAfter(editLink);
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
291 次 |
| 最近记录: |