当用户使用JQuery访问哈希链接(#something)时如何运行JavaScript函数? [英] How to run a JavaScript function when the user is visiting an hash link (#something) using JQuery?
本文介绍了当用户使用JQuery访问哈希链接(#something)时如何运行JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 http://example.com/app
上有一个webb应用程序,如果用户正在访问,我想显示一个表单 http:// example / app #add-item
。
有什么方法可以使用jQuery脚本来添加这个功能吗? / p>
我当前的jQuery脚本已经有这样的结构:
$ (文件).ready(
function(){
$('#search')。keyup(
function(){...}
);
}
)
如何使用这样的方式显示表单:
$(document).ready(
function(){
$('#search')。keyup(
function(){...}
);
$('#add-item')。visit(// .visit可能不存在
function(){content.innerHTML = myForm;}
);
}
)
解决方案
这是som我做的事情:
var hash =(window.location.hash).replace('#','');
if(hash.length == 0){
// no hash
}
else {
//使用`hash`
//示例:
if(hash =='add-item'){
//做点什么
}
}
I have a webb application at http://example.com/app
and I would like to show a form if the user is visiting http://example/app#add-item
.
Is there any way I can use a jQuery script to add this functionallity?
My current jQuery script already has a structure like this:
$(document).ready(
function() {
$('#search').keyup(
function() { ... }
);
}
)
How can I show a form using someting like this:
$(document).ready(
function() {
$('#search').keyup(
function() { ... }
);
$('#add-item').visit( // .visit probably doesn't exist
function() { content.innerHTML = myForm; }
);
}
)
解决方案
Here is something I do:
var hash = (window.location.hash).replace('#', '');
if (hash.length == 0) {
//no hash
}
else {
//use `hash`
//example:
if(hash == 'add-item'){
//do something
}
}
这篇关于当用户使用JQuery访问哈希链接(#something)时如何运行JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文