Jaki jest najszybszy sposób, aby uzyskać ciąg znaków typu "zanikanie" z klas w elemencie poniżej?jQuery - Zdobądź klasę elementów na podstawie prefiksu
<div class="MyElement fx-fade"> ... </div>
Jaki jest najszybszy sposób, aby uzyskać ciąg znaków typu "zanikanie" z klas w elemencie poniżej?jQuery - Zdobądź klasę elementów na podstawie prefiksu
<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];
}
}
dzięki: P Kocham Cię: D – Alex
Sprawdź JQuery selector regular expressions. To może być dokładnie to, czego potrzebujesz! :)
Myślę, że zrobiłeś to samo co ja. Zły odczytałem pytanie, odpowiedziałem na nie, a następnie skasowałem odpowiedź. OP nie pyta, jak filtrować elementy, biorąc pod uwagę dopasowanie częściowe. Myślę, że biorąc pod uwagę ten element, OP pyta, jak wyodrębnić część tego atrybutu. –
Ah! W takim przypadku przepraszam za zamieszanie. – Sai
Jeśli chciał szukać czegoś, który zakończył się w „wyciszeniem” użyłbyś:
$("*[class$='fade']")
A dla elementów z klasą, który rozpoczął się z „wyciszeniem” użyłbyś:
$("*[class^='fade']")
i uzyskać elementy zawierające „fade” należałoby użyć (byłoby szybciej niż przejście przez ciąg nazwy klas)
$("*[class*='fade']")
"*" pobiera wszystkie elementy, aby można było zastąpić to elementem, który chciał.
Jeśli chcesz elementy, które ma classname, który zaczyna się od „fx-” zrobiłbyś:
var classname = "";
var elArray = $("*[class*='fx-']");
for (var a= 0; a < elArray .length; a++)
{
//fade
classname = elArray[a].split("-")[1];
}
Tablica stosowany w pętli for musiałyby wszystkie elementy o classnames jak „fx-” .
Zamiast pętli for sprawdza elementy pod kątem poprawnej nazwy klasy.
Więcej informacji na jquery.com
+1 dla rozwiązania jQuery – HurnsMobile
elArray [a] .split nie jest funkcją? – bitstrider
Nie działa, gdy istnieje wiele klas. – powtac
pewnie bym iść z czymś takim:
//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];
}
}
Ostrożnie, 'klasa' jest słowem zastrzeżonym w javascript i spowoduje błąd w niektórych przeglądarkach. –
Ups, miał przeczucie, że to możliwe. Dzięki! – Jake
Ten prosty fragment używamy w naszym witryny:
/**
* 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;
});
});
Co by to pomogło? Nie jestem pewien, co masz na myśli. –