使用“#”的替代方法在 href 属性中
标签用于创建超链接,但在 jQuery 和 Ajax 的时代,我们使用它将 HTML 加载到同一页面的
也就是说,我们将 href
属性设置为 href="#"
,使用或滥用 #
字符作为占位符以及一些不需要的内容副作用,例如 URL 附加了 #
字符。
如果将 href
属性留空 href = ""
,则该链接似乎不起作用。
无论如何,是否可以以更简洁的方式执行此操作,例如当用户将鼠标悬停在链接上时在浏览器的状态栏中显示一些文本或虚拟功能,并使链接按照程序员的意图进行操作?
这是我的代码。
<ul id="sidebarmenu1">
// List that is converted into a menu...
<li> <a href="#" id="loadHotel" > HOTEL </a> </li>
<li> <a href="#" id="loadCountry"> COUNTRY </a> </li>
<li> <a href="#" id="loadCity"> CITY </a> </li>
</ul>
// The jQuery to load content into another div with Ajax
var loadUrl = "createHotel.php";
$("#loadHotel").click(function() {
$("#mainContent").html(ajax_load).load(loadUrl);
});
// ajax function to load hotel ---> rooms page
var url_loadRooms = "viewRooms.php";
$("#createRooms").click(function() {
$("#mainContent").html(ajax_load).load(url_loadRooms);
});
除了 "#"
之外,我还可以使用什么来使我的代码整洁......?
The <a>
tag is used to create hyperlinks but in this age of jQuery and Ajax we are using it to load HTML into <div>
s in the same page as the <a>
tags.
That said, we set the href
atribute as href="#"
, using or rather abusing the #
character as a placeholder along with some undesirable side effects like the URL getting appended with the #
character.
And if you leave the href
attribute blank href = ""
, the link does not seem to work.
Is there anyway to do this in a cleaner way like showing some text or dummy function in the status bar of the browser when the user hovers over the link and yet make the link do what the programmer intended?
Here's my code.
<ul id="sidebarmenu1">
// List that is converted into a menu...
<li> <a href="#" id="loadHotel" > HOTEL </a> </li>
<li> <a href="#" id="loadCountry"> COUNTRY </a> </li>
<li> <a href="#" id="loadCity"> CITY </a> </li>
</ul>
// The jQuery to load content into another div with Ajax
var loadUrl = "createHotel.php";
$("#loadHotel").click(function() {
$("#mainContent").html(ajax_load).load(loadUrl);
});
// ajax function to load hotel ---> rooms page
var url_loadRooms = "viewRooms.php";
$("#createRooms").click(function() {
$("#mainContent").html(ajax_load).load(url_loadRooms);
});
What else can I use instead of "#"
to make my code neat..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最好的解决方案是根本不使用一些虚拟占位符。使用有意义的 URL,如果实际点击该链接,它将向您显示从 AJAX 请求中获得的信息。
我经常对我的网络应用程序执行此操作,使用 Javascript 来增强工作网站。例如,HTML:
Javascript (jQuery):
这样做的好处很多。屏幕阅读器、网络爬虫和关闭 javascript 的用户都可以访问您的网站,甚至那些打开了 javascript 的用户也会在状态栏中获得有意义的 URL,以便他们知道自己要去哪里。
The best solution is to not use some dummy placeholder at all. Use a meaningful URL which, if the link were actually followed, would show you the information you'd get from the AJAX request.
I do this regularly with my web apps, using Javascript to enhance a working site. For example, the HTML:
The Javascript (jQuery):
The benefits of this are numerous. Your site is accessible to screen readers, web crawlers and users with javascript off, and even those with javascript turned on will get a meaningful URL in their status bar, so they'll know where they're going.
我通常使用这个:
将锚点的
href
属性设置为javascript:void(0);
向浏览器指示该锚点不是指向另一个文档或锚点的超链接,I usually use this:
Setting an anchor's
href
attribute tojavascript:void(0);
indicates to the browser that this anchor is not a hyperlink to another document or anchor,如果您的
onclick
处理程序返回false
,则浏览器不会跟踪该链接。试试这个:编辑:
如果你绝对不使用 octothorpe (即
#
符号),你不必这样做。试试这个:If your
onclick
handler returnsfalse
, the link won't get followed by the browser. Try this:EDIT:
If you're absolutely hellbent on not using the octothorpe (ie.
#
symbol), you don't have to. Try this:为什么需要在 href 中定义任何内容?
这就是SO的工作原理=>
但是 - 如果链接实际上应该导航到某个地方 - 保持正常的 href
以及 e.preventDefault() 与 jQuery 的常规行为。
Why you need anything to be defined in href?
That's how SO works=>
But - if link is supposed to actually navigate somewhere - keep normal href
and just e.preventDefault() regular behavior with jQuery.
也许我不明白,但如果没有链接,你就不应该使用 元素放在首位。使用一些>或将事件侦听器附加到列表元素。您可以使用 CSS 将这些元素设置为
cursor:pointer;
。请记住,浏览器有一些与链接相关的特殊操作,例如“在新选项卡中打开”、“保存目标元素”等。当您使用虚拟
href=''
属性时,这些操作会以损坏的方式工作,所以最好根本不使用链接。另一方面,如果您能够将这些 ajaxified 部分的内容呈现为普通页面(并且这是有意义的),请遵循 nickf 的建议。
Maybe I don't get smething, but if there is no link, you just shouldn't use an <a /> element in the first place. Use some <span /> or attach event listeners to list elements. You can style these elements to have
cursor: pointer;
using CSS.Remember that browsers have some special actions associated with links, like "open in new tab", "save target element" etc. When you use dummy
href=''
attribute these actions work in a broken way, so it's better to not use links at all.On the other hand, if you are able to render content of these ajaxified parts as normal pages (and it makes sense), follow nickf's advice.
nickf 打败了我;然而,有几件事值得一提。 您永远不应该对链接使用“javascript”协议(除非您打算这样做)成为一个书签)。它与渐进增强相反。只要有可能,
href
属性就应该是实际的 URI 资源,以便它可以正常降级。在所有其他情况下,鼓励使用“#”,并应使用适当的 JavaScript 来防止页面滚动到顶部。有两种方法可以做到这一点。在单击处理程序中,要么阻止默认操作,要么返回 false。或者
nickf beat me to it; however, a few things should be mentioned. You should never use the "javascript" protocol for a link (unless maybe you intend for it to be a bookmarklet). It is the opposite of progressive enhancement. Whenever possible, the
href
attribute should be an actual URI resource so that it can gracefully degrade. In all other situations, "#" is encouraged and proper JavaScript should be used from preventing the page from scrolling to the top. There are two methods to do so. In the click handler, either prevent the default action, or return false.or
使用“#”字符作为占位符基本上会使链接“活动”。浏览器将其解释为指向其他内容的标签。如果 href 为空,浏览器将假定 a 标签只是另一个标签。
解决这个问题的方法是将一些 CSS 分配给模拟 a 标签相同功能的不同标签。悬停时,更改鼠标、下划线、更改颜色等。您可以轻松更改窗口状态,并使用户看起来像是在单击链接,而实际上他们并没有单击链接,而是发出了单击事件。
事实上,这是更好的选择,因为通过事件绑定仅运行一个没有 JavaScript 就无法使用的 JS 函数,一开始就不应该被视为链接。
Using the "#" character as a placeholder basically makes the link "active." The browser interprets it as a tag that points to something else. If href is empty, the browser will assume the a tag is just another tag.
A way around that is to assign some CSS to a different tag that emulates the same functionality of the a tag. On hover, change the mouse, underline, change color, etc. You can easily change the window status and make it seem like the user is clicking on a link when in reality they aren't clicking a link so much as making a click event.
In fact, that's the better option, because running only a JS function through event binding that can't be used without JavaScript shouldn't be considered a link in the first place.
我总是使用这个:
Click to your heart's pleasure
I always use this:
<a href="javascript:;">Click to your heart's delight</a>