[英]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. 在我们讲话时,我正在努力工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.