我尝试使用标签执行 jquery 函数。首先我尝试这样。
<a href="#about_us" onclick="aboutus()">About US</a>
这是我在单独文件中的 jquery 函数。
function aboutus() {
$("#content").css({
"display": "block",
"opacity" : 1
});
$(".about_us_one").css({
"left": ($w - 980) / 2,
"top": 120,
"z-index": 1000,
"display": "block"
});
$(".about_us_one").animate({
"opacity": 0.90,
"width": 980
}, 2500, $easing2, function() {
});
var $about_us_one, $contentHeight;
$about_us_one = $(".about_us_one").height();
$(".about_us_two").css({
"left": ($w - 980) / 2,
"top": 140 + $about_us_one,
"z-index": 1000,
"display": "block"
});
$(".about_us_two").animate({
"opacity": 0.90,
"width": 980
}, 2500, $easing2, function() {
});
$contentHeight = $(document).height();
$("#bfooter").animate({
"top": $contentHeight,
"margnin-top": 15
});
$("#splash").animate({
"top": 0,
"opacity": 0.75,
"height": 100
}, 1500, $easing2, function() {
});
$(".menu").animate({
"opacity": 1,
"top": 50,
//"margin-top": 50
// "height": $h
}, 500, $easing);
$("#slogn1").animate({
"opacity": 1,
"top": 50,
//"margin-top": 50
// "height": $h
}, 500, $easing);
$("#slogn2").animate({
"opacity": 1,
"top": 20,
//"margin-top": 50
// "height": $h
}, 500, $easing);
}
使用此方法时一切正常。但如果我刷新页面。然后我必须再次单击“关于我们”链接才能查看内容。
所以这不是我真正需要做的事情。所以我尝试像这样使用 php 从标签传递变量。
<a href="http://localhost/eventztable/home.php?name=about_us">About US</a>
$page = $_GET['name'];
if ($page == "about_us") {
?>
<script>
aboutus();
</script>
<?php
}
但是在这个方法中什么也没有发生。所以我觉得应该是有问题的。所以我在“关于我们”功能中放置了一个警报(“测试”)。但是刷新时会弹出该警报。但其他代码没有执行。
我真的厌倦了做这做那。请帮我解决这个问题。如果有任何简单的方法请告诉我。
谢谢你..
最佳答案
以此为例:
<a href="#aboutus" onclick="aboutus()">About Us</a>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function aboutus() {
alert(123);
}
$(document).ready(function(){
if(window.location.hash == '#aboutus') {
$('a[href="#aboutus"]').trigger('click');
}
});
</script>
单击链接后(初始单击)。当然,它会执行,然后当你检测到哈希值时(在本例中是#aboutus
),它会再次执行,(在本例中是alert()
在 aboutus()
函数内)
关于javascript - 使用 anchor 标记执行jquery函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313390/