简体   繁体   English

使用jQuery显示隐藏的DIV(无效)

[英]Show hidden DIV with jQuery (Not working)

Language: jQuery / Javascript / PHP 语言: jQuery / Javascript / PHP

I am trying to show a hidden DIV once a user clicks on a link. 我试图在用户单击链接后显示隐藏的DIV。

There are 3 types of actions that should take place, depending on the value inside data-action attached inside the href tag. 根据href标记内附加的data-action的值,应执行3种类型的操作。

It has 3 possible values: 它具有3个可能的值:

  • shake 抖动
  • bounce 弹跳
  • default (my problem) 默认 (我的问题)

Now using switch , I switch through these actions in the Javascript code. 现在使用switch ,我将在Javascript代码中切换这些动作。

My problem is, I can't show the hidden div I am trying to target (code under default is where I'm running into some trouble with). 我的问题是,我无法显示我要定位的隐藏div( 默认情况下 ,我遇到麻烦的地方是代码)。

JSfiddle: http://jsfiddle.net/ryxcw/1/ (problem starts at line 59) JSfiddle: http : //jsfiddle.net/ryxcw/1/ (问题从第59行开始)

Javascript code in use: 使用的Javascript代码:

function loadBubbleActions() {
  $('#container > a').each(function () {

    switch ($(this).attr('data-action')) {
      case "shake":
        //bind shake action to bubble
        $(this).live("click", function (e) {


          var props = $.parseJSON($(this).attr('data-action-properties'));
          var ox = $(this).css('left').replace('px', '');
          var oy = $(this).css('top').replace('px', '');
          if ($('#tae').length == 0) {

            var overlay = jQuery('<div id="overlay" style="background-color: #fff !important; background-image: url(images/texture-shake.jpg);"> </div>');
            overlay.appendTo(document.body)

            $(document.body).append('<div id="tae" class="shake" style="position:absolute;opacity:1;z-index:12000;">Info goes here</div>');

            $('#overlay').click(function () {

              $('#overlay').remove();
              $('#tae').remove();

            });


            var cssWidth = ($(this).css('width').replace('px', '') / 2 - 20);
            var cssHeight = ($(this).css('height').replace('px', '') / 2 - 20);
            var ss = ($(window).width() / 2);
            var dd = ($(window).height() / 2);

            $('#tae').css('left', (ss - cssWidth) + "px");
            $('#tae').css('top', (dd - cssHeight) + "px");


            $('#tae').effect("shake", {
              times: 5
            }, 500);
          } else {
            $('#tae').effect("shake", {
              times: 5
            }, 500);

          }
        });

        break;



      case "bounce":
        //do specific action stuff here

        $(this).live("click", function (e) {
          alert("This is a bouncing post " + $(this).attr('data-link'));
        });
        break;
      default:
        //do action code for default here
        $(this).live("click", function (e) {

          divID = $(this).attr('id');

          var props = $.parseJSON($(this).attr('data-action-properties'));
          var act = $(this).attr('data-action');
          var ox = $(this).css('left').replace('px', '');
          var oy = $(this).css('top').replace('px', '');
          if ($('.panda').length == 0) {

            $("#theDIV-" + divID).show();
            $("#overlay").show();

            $('#overlay').click(function () {

              $('#overlay').remove();
              $('.panda').remove();

            });


            var cssWidth = ($(this).css('width').replace('px', '') / 2 + 400);
            var cssHeight = ($(this).css('height').replace('px', '') / 2 - 20);
            var ss = ($(window).width() / 2);
            var dd = ($(window).height() / 2);

            $('.panda').css('left', (ss - cssWidth) + "px");
            $('.panda').css('top', "100px");
          } else {
            //$('#tae').effect("shake", { times:props.shakeNumber }, 200);
          }

        });
    }
  });
}

I really hope someone would be able to help out, I've spent the entire night nitpicking on this thing, I could really use the guidance. 我真的希望有人能够提供帮助,我花了一整夜的时间在研究这件事,我真的可以使用指导。 Thank you so much for your time! 非常感谢您的参与!

Once again, here is a JsFiddle for your convenience -- 再次为您提供方便的JsFiddle-
JSfiddle: http://jsfiddle.net/ryxcw/1/ (problem starts at line 59) JSfiddle: http : //jsfiddle.net/ryxcw/1/ (问题从第59行开始)

You can't use .live() when you generate a jQuery object with anything other than a CSS selector string. 当您使用除CSS选择器字符串之外的任何内容生成jQuery对象时,都不能使用 .live() That is, 那是,

 
 
 
  
  $(this).live(whatever)
 
  

will never work. 将永远无法工作。 (well that's not true; .live() doesn't work without a selector string if you're trying to delegate, but when the handler is there on the element directly it works. It's still the case that it's somewhat weird, and .live() is deprecated anyway :-) (嗯,这不是真的; 如果您要委托的话, .live()在没有选择符字符串的情况下将无法工作,但是当处理程序直接位于元素上时,它就可以工作.live()仍然被弃用:-)

Also, you seem to want to find the <div> by looking at the clicked element's "id" value. 另外,您似乎想通过查看被单击元素的“ id”值来找到<div> That doesn't make much sense, because you can't give the same "id" to two different elements (well you can but things don't work very well). 这没有多大意义,因为您不能为两个不同的元素赋予相同的“ id”(可以,但是事情做得不好)。 In any case, you haven't given the <a> tag an "id" value anyway. 无论如何,您都不会给<a>标记一个“ id”值。 ( edit - now I see; you're using the "id" on the <a> as part of the <div> "id". Still, there's no "id" on that <a> element.) 编辑 -现在我明白了,你是在使用上的“ID” <a>作为一部分 <div> “ID”不过,没有上“身份证”。 <a>元素。)

There really are a lot of things wrong in that code. 该代码中确实存在很多错误。 I think you should start with something a lot simpler and work up. 我认为您应该从简单得多的工作入手。

You're doing this 你在做这个

if ($('.panda').length==0) {
      $("#theDIV-" + divID).show();

but $('.panda').length returns 1 because 但是$('.panda').length返回1是因为

<div class="panda hiddenDIV" id="theDIV-77" style="position:absolute;z-index:12000;">This is the content</div>

exists so the action $("#theDIV-" + divID).show(); 存在,因此操作$("#theDIV-" + divID).show(); won't run. 不会跑。

http://jsfiddle.net/ryxcw/2/ http://jsfiddle.net/ryxcw/2/

ASIDE 在旁边

Where you've used $(this).live() you should have used $(this).on() (though live will still work there). 您在使用$(this).live()应该使用$(this).on() (尽管live仍然可以在其中使用)。

You're using data wrong. 您使用的数据有误。 data-action="something" is stored in the DOM on run. data-action="something"在运行时存储在DOM中。 You access its contents using .data('action') not .attr('data-action') 您使用.data('action')而不是.attr('data-action')访问其内容

Also I have to agree with Pointy, kind of. 我也必须同意Pointy的那种。 There's not so much "a lot of things wrong with the code" but rather, you're doing a lot of things wrong. 没有太多“代码有很多错误”,而是您在做很多错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM