douju4594
douju4594
2016-03-02 08:13

如何将变量从javascript传递到jquery?

I have the following code,

<a href="#" onclick="popup('<?php echo $temp_class['class']; ?>')">

I pass a class generated from php function to javascript function popup().

I have another code

  function popup (myclass) {
    $(myclass).hide();
  }

The variable myclass accept value passed to the function. How to use myclass value to jquery so that I can hide an html element associated with the given class like example above?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • donglian6625 donglian6625 5年前

    The dot is used for classes so I think this will work:

    $('.' + myclass).hide();
    
    点赞 评论 复制链接分享
  • douliedai4838 douliedai4838 5年前

    Expanding on Wouter's answer a bit.

    The dot class is needed for classes but it might also be worth checking if it already exists.

    function popup (myclass) {
        if(myclass.charAt(0) === '.'){
            //If we already have a '.' just hide.
            $(myclass).hide();
        } else {
            $('.' + myclass).hide();
        }
    }
    

    This will make the function more flexible.

    点赞 评论 复制链接分享
  • douniang3866 douniang3866 5年前

    Did you think to add . to your selector ?

    Because in jquery, the selector for class named myClass is .myClass like in css.

    点赞 评论 复制链接分享

相关推荐