仅在首次访问时显示>if(!localStorage.getItem(“runOnce”)){触发锚链接

Display only on first visit > if( ! localStorage.getItem( "runOnce" ) ) { trigger anchor link

本文关键字:runOnce getItem 链接 localStorage 显示 访问 gt if      更新时间:2023-09-26

我有一个javascript表单,可以进行一些计算,但只想在访问者第一次进入网站时显示。

我试图在我的代码之前添加

jQuery(document).ready(function($) {
   if( ! localStorage.getItem( "runOnce" ) ) {

我需要触发一个链接,而不是runOnce。你能就如何做到这一点提出建议吗???我从0开始学习…感谢

data-slide标记是特定JS脚本的自定义标记。当然,这是在网站中产生"滑动"效果的代码。我一直在查看网站。使用"关于"按钮

您有一个滑动到容器834的about按钮。将id about-btn添加到其中,如下所示:

<a id="about-btn" href="http://www.inlovewithdeath.com/about-satish-modi-the-author/" class="menu-item menu-item-type-post_type menu-item-object-page" data-slide="container-834" data-name="about-satish-modi-the-author">About</a>

现在将其添加到某个地方,很可能是在外部脚本中,然后将其包括在内:

//  shortcut of $(document).ready().
$(function() {
    //  check if runOnce exists, if not run the block.
    if (! localStorage.getItem('runOnce')) {
        //  we set the runOnce, so this block doesn't run on the second time.
        localStorage.setItem('runOnce', '1');
    }
    else
    {
        // The item exists and set!!
        //  Now we will simulate a click on the about button.
        //  Which should move the user to your spot with your code.
        $('#about-btn').click();
    }
});

我建议先学习纯/香草JS,然后再使用框架来了解一切工作原理。

JQuery支持"点击"、"提交"或"触发器"方法,可用于触发操作。假设你可以识别表单本身(启动"提交"操作)或"点击"表单中的提交按钮,它可能看起来像:
// force the form identified as id=myform to submit
$("#myform").submit();
// or click the submit button on the form
$("#mysubmitbutton").click();

submit():http://api.jquery.com/submit/单击():http://api.jquery.com/click/