Какой самый быстрый способ получить строку типа "fade" из классов в элементе ниже?
<div class="MyElement fx-fade"> ... </div>
Какой самый быстрый способ получить строку типа "fade" из классов в элементе ниже?
<div class="MyElement fx-fade"> ... </div>
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];
}
}
Если вы хотите найти что-то, что закончилось "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
Попробуйте следующее:
$("div[class*='fade']")
Зайдите 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];
}
}
Этот простой фрагмент, который мы используем на наших сайтах:
/**
* Script to load a popup container for share buttons
*
* Possible usage for Facebook, Twitter and Google:
*
* <a class="share-buttons-fb" href="#" onclick="location.href='https://www.facebook.com/sharer/sharer.php?u=<?php echo get_the_permalink(); ?>&title=<?php the_title(); ?>'; return false;">Facebook</a>
* <a class="share-buttons-twitter" href="#" onclick="location.href='https://twitter.com/intent/tweet?text=<?php the_title(); ?>: <?php echo get_the_permalink(); ?>'; return false;">Twitter</a>
* <a class="share-buttons-google" href="#" onclick="location.href='http://plus.google.com/share?url=<?php echo get_the_permalink(); ?>'; return false;">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;
});
});