javascript - 使用 anchor 标记执行jquery函数

标签 javascript php jquery html

我尝试使用标签执行 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/

相关文章:

javascript - 使用 Google Tag Manager 动态生成 Schema/JSON-LD 标签,但不断收到以下错误 : ' unparsable structured data'

javascript - 为同名的多个类抓取浮点值

javascript - HighCharts Linechart MySQL/PHP 多行数据

javascript - 动态添加的元素不会触发 Backbone JS 事件

php - 无法使用 jquery 检索 json 数据

JavaScript:箭头函数是一等函数吗?

javascript - D3 按设定的时间间隔更新/重绘 SVG

php - 如何使用Slim Router在PHP MVC中向用户显示错误消息?

php - Woocommerce 退款电子邮件

jquery - 为什么我在浏览器 Firefox 中的占位符后没有红星?