【发布时间】:2011-03-13 09:20:31
【问题描述】:
从下面元素中的类中获取像“fade”这样的字符串的最快方法是什么?
<div class="MyElement fx-fade"> ... </div>
【问题讨论】:
-
这对您有什么帮助?我不确定你的意思。
标签: javascript jquery
从下面元素中的类中获取像“fade”这样的字符串的最快方法是什么?
<div class="MyElement fx-fade"> ... </div>
【问题讨论】:
标签: javascript jquery
如果您想查找以“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
【讨论】:
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];
}
}
【讨论】:
【讨论】:
题目和题目不完全匹配(题目要求前缀,题目要求后缀/部分)
结合以下两个 jQuery/CSS 选择器:
$( "[class^='fx-'],[class*=' fx-']" )
[class^='fx-']Starts-With-Selector<a class="fx-fade MyElement anything">
[class*=' fx-']Contains-Selector<a class="MyElement fx-fade anything">
此选择器可确保您不会收到误报,例如 <a class="MyElement sfx-none">
几乎与前缀选择器相同:
$( "[class$='-fade'],[class*='-fade ']" )
[class$='-fade']End-With-Selector<a class="MyElement anything fx-fade">
[class*='-fade ']Contains-Selector<a class="anything fx-fade MyElement">
此选择器可确保您不会收到误报,例如 <a class="MyElement sfx-none">
当您需要查找既不是类名的开头也不是结尾的子字符串时,请使用其他答案中建议的选择器:
$( "[class*='fade']" )
[class*='fade']Contains-Selector<a class="fx-fade-out"> 警告:这也会找到"no-fade" 或"faded"。小心使用 Contains-Selector!
【讨论】:
查看JQuery selector regular expressions。它可能正是您所需要的! :)
【讨论】:
我可能会选择类似的东西:
//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中的保留字,在某些浏览器中会导致错误。
我们在网站中使用的这个简单的 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;
});
});
【讨论】: