【问题标题】:jQuery - Get a element class based on a prefixjQuery - 基于前缀获取元素类
【发布时间】:2011-03-13 09:20:31
【问题描述】:

从下面元素中的类中获取像“fade”这样的字符串的最快方法是什么?

<div class="MyElement fx-fade"> ... </div>

【问题讨论】:

  • 这对您有什么帮助?我不确定你的意思。

标签: javascript jquery


【解决方案1】:

如果您想查找以“fade”结尾的内容,您可以使用:

$("*[class$='fade']")

对于类以“fade”开头的元素,您可以使用:

$("*[class^='fade']")

要获取包含“fade”的元素,您将使用(这比通过类名字符串更快)

$("*[class*='fade']")

* 获取所有元素,因此您可以将其替换为您想要的元素。

如果你想要类名以fx- 开头的元素,你可以这样做:

var classname = "";
var elArray = $("*[class*='fx-']");
    
for (var a = 0; a < elArray.length; a++) {
   // fade
    classname = elArray[a].split("-")[1]; 
}

for 循环中使用的数组将包含类名如“fx-”的所有元素。

而不是 for 循环检查元素的正确类名。

更多信息jquery.com

【讨论】:

  • elArray[a].split 不是函数?
【解决方案2】:
var classes = $('.MyElement').attr('class').split(' ');
for (var i = 0; i < classes.length; i++) {
  var matches = /^fx\-(.+)/.exec(classes[i]);
  if (matches != null) {
    var fxclass = matches[1];
  }
}

【讨论】:

    【解决方案3】:

    试试这个:

    $("div[class*='fade']") 
    

    More info

    【讨论】:

      【解决方案4】:

      题目和题目不完全匹配(题目要求前缀,题目要求后缀/部分)


      1。按类前缀选择元素

      结合以下两个 jQuery/CSS 选择器:

      $( "[class^='fx-'],[class*=' fx-']" )
      
      • [class^='fx-']Starts-With-Selector
        查找整个类属性的第一个字母以“fx-”开头的元素
        例如&lt;a class="fx-fade MyElement anything"&gt;
      • [class*=' fx-']Contains-Selector
        查找在类属性内的任意位置找到 空格 + "fx-" 的元素
        例如&lt;a class="MyElement fx-fade anything"&gt;

      此选择器可确保您不会收到误报,例如 &lt;a class="MyElement sfx-none"&gt;


      2。按类后缀选择元素

      几乎与前缀选择器相同:

      $( "[class$='-fade'],[class*='-fade ']" )
      
      • [class$='-fade']End-With-Selector
        使用“-fade”查找整个类属性的最后一个字母的元素
        例如&lt;a class="MyElement anything fx-fade"&gt;
      • [class*='-fade ']Contains-Selector
        查找在类属性内的任意位置找到 "-fade" + 空格 的元素
        例如&lt;a class="anything fx-fade MyElement"&gt;

      此选择器可确保您不会收到误报,例如 &lt;a class="MyElement sfx-none"&gt;


      3。按类子字符串选择元素

      当您需要查找既不是类名的开头也不是结尾的子字符串时,请使用其他答案中建议的选择器:

      $( "[class*='fade']" )
      
      • [class*='fade']Contains-Selector
        查找 "fade" 在类属性内的任意位置找到的元素
        例如&lt;a class="fx-fade-out"&gt;

      警告:这也会找到"no-fade""faded"。小心使用 Contains-Selector!

      【讨论】:

        【解决方案5】:

        查看JQuery selector regular expressions。它可能正是您所需要的! :)

        【讨论】:

        • 我想你和我做的一样。我误读了这个问题,回答了它,然后删除了我的答案。 OP 没有询问如何在给定部分匹配的情况下过滤元素。我认为给定元素,OP 正在询问如何提取部分属性。
        • 啊!在这种情况下,我为造成的混乱道歉。
        【解决方案6】:

        我可能会选择类似的东西:

        //Split class list into individual classes:
        var classes = $(".MyElement").attr("class").split(" ");
        var fxType;
        
        //Loop through them:
        for (var i = 0, max = classes.elngth; i < max; i++) {
          var class = classes[i].split("-");
          //Check if the current one is prefixed with 'fx':
          if (class[0] == "fx") {
            //It is an FX - do whatever you want with it, the type of FX is stored in class[1], ie:
            fxType = class[1];
          }
        }
        

        【讨论】:

        • 小心,class是javascript中的保留字,在某些浏览器中会导致错误。
        • 糟糕,有一种感觉。谢谢!
        【解决方案7】:

        我们在网站中使用的这个简单的 sn-p:

        /**
         * Script to load a popup container for share buttons
         *
         * Possible usage for Facebook, Twitter and Google:
         *
         * <a class="share-buttons-fb" href="https://www.facebook.com/sharer/sharer.php?u=<?php echo get_the_permalink(); ?>&title=<?php the_title(); ?>">Facebook</a>
         * <a class="share-buttons-twitter" href="https://twitter.com/intent/tweet?text=<?php the_title(); ?>: <?php echo get_the_permalink(); ?>">Twitter</a>
         * <a class="share-buttons-google" href="http://plus.google.com/share?url=<?php echo get_the_permalink(); ?>">Google+</a>
         */
        jQuery(document).ready(function ($) {
        
            // Whenever an anchor with the class with share-button in it is clicked
            $("a[class*='share-button']").click(function () {
                // Variables to set the size of the popup container
                var windowWidth = 500;
                var windowHeight = 255;
        
                // Variables to set the position of the popup container
                var positionWindowLeft = (screen.width / 2) - (windowWidth / 2);
                var positionWindowTop = (screen.height / 3) - (windowHeight / 3);
        
                // Create new windows with the opening url of the href attribute of the anchor and the above variables
                var popupWindow = window.open($(this).prop('href'), '', 'scrollbars=no,menubar=no,status=no,titlebar=no,toolbar=nolocation=no,menubar=no,resizable=noe,height=' + windowHeight + ',width=' + windowWidth + ',top=' + positionWindowTop + ', left=' + positionWindowLeft);
        
                // If the default windows is focused
                if (window.focus) {
                    // Change the focus to the the popup window
                    popupWindow.focus();
                }
        
                return false;
            });
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-12-14
          • 1970-01-01
          • 2011-05-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-30
          • 1970-01-01
          相关资源
          最近更新 更多