简体   繁体   English

如果传递了URL参数,则使用jQuery显示div

[英]Show div with jQuery if a URL parameter is passed

I have these divs set up on all my pages to catch if the action was a success or not through a URL parameter. 我在所有页面上都设置了这些div,以通过操作成功或失败通过URL参数进行捕获。 The error div is supposed to show when there's an error - and the success div, when it's a success. 错误div应该在出现错误时显示-成功div在成功时显示。

It seems to work fine on other pages, except this one. 除了这一页以外,它在其他页面上似乎工作正常。 I've gone through it several times and can't figure out what might be wrong. 我已经经历了好几次了,无法弄清楚可能出了什么问题。

In firebug console, I get this: ReferenceError: $ is not defined $('#action-success').show(); 在Firebug控制台中,我得到以下信息:ReferenceError:$未定义$('#action-success')。show();

CSS: CSS:

    /* error div */
    div#action-error
    {
        text-align: center;
        border: 1px solid white;
        display: none;
        background-color: #a80077;
        border-radius: 15px;
    }

    /* success div */
    div#action-success
    {
        text-align: center;
        border: 1px solid white;
        display: none;
        background-color: #5a8e4a;
        border-radius: 15px;
    }

HTML: HTML:

    <!-- RESULT ERROR/SUCCESS -->
    <div class="container_12" id="action-error">
        <span class="error">Error: There was an error deleting your album. Please try again.</span>
    </div>
    <div class="container_12" id="action-success">
       <span class="success">Your album was deleted successfully.</span>
    </div>
    <!-- RESULT ERROR/SUCCESS END -->

Jquery: jQuery:

Top of page: (found on SO) 页面顶部:(位于SO上)

        <script>
            function getParameterByName(name) {
                return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
            }
        </script>

Bottom: 底部:

        //------------------------------------------------------------------
        var error = getParameterByName('error');

        if(error){
            alert(error);
            $('#action-error').show();
        }
       //------------------------------------------------------------------
        var success = getParameterByName('result');

        if(success){
            $('#action-success').show();
        }

I've looked at the other topics on SO, but none of them seem to be exactly what my issue is. 我看了关于SO的其他主题,但似乎都不是我的问题所在。 Any help would be appreciated, and thank you for your time. 任何帮助将不胜感激,并感谢您的时间。

Have you wrapped the JavaScript with a function that passes jQuery as $? 您是否用将$传递给jQuery的函数包装了JavaScript?

(function($){
    //-----------
    var error = getParameterByName('error');

    if(error){
        alert(error);
        $('#action-error').show();
    }
   //------------------------------------------------------------------
    var success = getParameterByName('result');

    if(success){
        $('#action-success').show();
    }
    //------------

})(jQuery);

well, folks - this is what happens when you code half-asleep going on coffee fumes. 好吧,伙计们-当您编写半睡着的咖啡烟雾代码时,就会发生这种情况。 The problem was I forgot to include the jQuery library in my file... Sorry to have wasted your time. 问题是我忘了在文件中包含jQuery库...很抱歉浪费您的时间。

I'm working on my dunce cap as we speak. 在我们讲话时,我正在努力工作。

节目<div>当满足特定的 URL 参数时</div><div id="text_translate"><p>我在 Google 表格中有一个表单,它将 URL 参数传递到页面,然后该页面应显示基于 URL 参数的内容。</p><p> 因此,如果有人在 Google Sheet 中检查“item 1”和“item 2”,参数将按如下方式传递:</p><p> <a href="https://my-url.com/?item1=value1&amp;item2=value2" rel="nofollow noreferrer">https://my-url.com/?item1=value1&amp;item2=value2</a></p><p> 现在有几个 div 容器,设置为 display:none 和 css。 当满足 URL 参数时,应该会显示隐藏的 div 容器。 所以 html 是:</p><pre> &lt;div class="hidden" id="value1"&gt;This content should show, when the URL parameter value 1 is passed&lt;/div&gt; &lt;div class="hidden" id="value2"&gt;This content should show, when the URL parameter value 2 is passed&lt;/div&gt;</pre><p> 我在网上找到了一些代码,它几乎可以做到这一点,但它一次只能显示一个 div: <a href="https://jennamolby.com/how-to-display-dynamic-content-on-a-page-using-url-parameters/" rel="nofollow noreferrer">https://jennamolby.com/how-to-display-dynamic-content-on-a-page-using -url参数/</a></p><p> 我的问题是,对于每个传递的参数,必须显示一个字段。</p><p> 谁能帮我这个? 无论如何,我真的不是 js 专家。</p></div> - Show <div> when a specific URL Parameter is met

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用作为URL参数传递的jQuery选择器-安全吗? - Using jQuery Selectors Passed as URL Parameter - Safe? 节目<div>当满足特定的 URL 参数时</div><div id="text_translate"><p>我在 Google 表格中有一个表单,它将 URL 参数传递到页面,然后该页面应显示基于 URL 参数的内容。</p><p> 因此,如果有人在 Google Sheet 中检查“item 1”和“item 2”,参数将按如下方式传递:</p><p> <a href="https://my-url.com/?item1=value1&amp;item2=value2" rel="nofollow noreferrer">https://my-url.com/?item1=value1&amp;item2=value2</a></p><p> 现在有几个 div 容器,设置为 display:none 和 css。 当满足 URL 参数时,应该会显示隐藏的 div 容器。 所以 html 是:</p><pre> &lt;div class="hidden" id="value1"&gt;This content should show, when the URL parameter value 1 is passed&lt;/div&gt; &lt;div class="hidden" id="value2"&gt;This content should show, when the URL parameter value 2 is passed&lt;/div&gt;</pre><p> 我在网上找到了一些代码,它几乎可以做到这一点,但它一次只能显示一个 div: <a href="https://jennamolby.com/how-to-display-dynamic-content-on-a-page-using-url-parameters/" rel="nofollow noreferrer">https://jennamolby.com/how-to-display-dynamic-content-on-a-page-using -url参数/</a></p><p> 我的问题是,对于每个传递的参数,必须显示一个字段。</p><p> 谁能帮我这个? 无论如何,我真的不是 js 专家。</p></div> - Show <div> when a specific URL Parameter is met 仅当url具有特定查询参数字符串jQuery时显示DIV - Show DIV only when url has specific query parameter string jQuery 为什么此URL参数不使用jQuery隐藏此div? - Why is this URL PArameter not hiding this div with jQuery? python django:如何根据传递给模板的参数确定div是否显示或隐藏 - python django: How can I determine if a div should show or hide based on the parameter passed to template 获取传递的 URL 参数并添加到 iframe URL - Grab URL parameter passed and add to iframe URL 根据网址显示div - Show div based on URL 根据网址显示div - Show div based on url 是否可以显示带有网址的div? - Is it possible to show a div with an url? 用Jquery替换div参数 - Replace div parameter with Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM