如何导航到内部链接并执行 jquery 函数?
我对 jquery 很陌生。 我正在使用这个 jquery 代码片段,它工作正常。但我想从网站上的任何位置进行导航,并且仍然可以使用该功能:
function showselectedbox(theselectedbox) {
$(".pageboxes").each(function (index) {
if ($(this).attr("id") == theselectedbox) {
$(this).show(200);
}
else {
$(this).hide(600);
}
});
}
<a id="myHeader1" href="javascript:showselectedbox('mynewboxes1');" >show this one only</a>
<div class="pageboxes" id="mynewboxes1">Div #1</div>
<a id="myHeader2" href="javascript:showselectedbox('mynewboxes2');" >show this one only</a>
<div class="pageboxes" id="mynewboxes1">Div #1</div>
...更多相同的内容。
我还为导航构建了一个选项卡式菜单,并希望标记所选的链接。
<div id="metaltop-teal">
<ul>
<li class="first active"><a id="myHeader1" href="javascript:showonlyone('mynewboxes1');" >show this one only</a></li>
<li><a id="myHeader2" href="javascript:showonlyone('mynewboxes2');" >show this one only</a></li>...
</ul>
</div>
还有其他方法来格式化 url 吗?例如:链接到页面
谢谢
I´m a very new to jquery.
I´m using this jquery code fragment and it´s working fine. But I would like to navigate from anywhere on the website and still have the function work:
function showselectedbox(theselectedbox) {
$(".pageboxes").each(function (index) {
if ($(this).attr("id") == theselectedbox) {
$(this).show(200);
}
else {
$(this).hide(600);
}
});
}
<a id="myHeader1" href="javascript:showselectedbox('mynewboxes1');" >show this one only</a>
<div class="pageboxes" id="mynewboxes1">Div #1</div>
<a id="myHeader2" href="javascript:showselectedbox('mynewboxes2');" >show this one only</a>
<div class="pageboxes" id="mynewboxes1">Div #1</div>
... more of the same.
I also build a tabbed menu for the navigation, and would like the selected link marked.
<div id="metaltop-teal">
<ul>
<li class="first active"><a id="myHeader1" href="javascript:showonlyone('mynewboxes1');" >show this one only</a></li>
<li><a id="myHeader2" href="javascript:showonlyone('mynewboxes2');" >show this one only</a></li>...
</ul>
</div>
Is there also another way to format the url? e.g: <a href="url" ...>link to page</a>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将 javascript 放入 .js 文件中,然后将该文件包含在其他文件中:
现在您可以自由引用您的 javascript 函数。
请记住在包含 jquery 之后包含您的 .js 文件,否则您的脚本将无法工作。
编辑:
为了让代码在页面加载时执行,我会使用 $('document').ready() 这可以确保在加载 DOM 的所有元素之前不会执行代码。这样,您就可以避免尝试操作不存在的元素。当使用事件处理程序(例如 click())时,这一点尤其重要。对于 live() 和 delegate() 也附加到稍后创建的元素,这并不那么重要。
首先,您创建一行:
$('document').ready(function(){
在该行之后,您可以在页面加载时放置要执行的任何内容。
要完成此块,您可以编写
});
然后你把你的函数等等。
you should put your javascript in a .js file and then include the file in your other files:
now you can refer to your javascript function freely.
Remember to include your .js-file AFTER you include jquery, or your script won't work.
EDIT:
To have code execute upon pageload, I'd use $('document').ready() This ensures that the code isn't executed until all elements of the DOM is loaded. This way, you avoid trying to manipulate elements that aren't there. This is especially important when using eventhandlers such as click(). For live() and delegate() which also are attached to elements created later, this is not so important.
Firstly you make a line:
$('document').ready(function(){
after that line, you put anything to execute when the page is loaded.
To finish this block, you write
});
And after that you put your functions etc.