Если вам действительно нужно или нужно использовать одно и то же имя класса из двух разных пакетов, у вас есть два варианта: 1-выбрать один для использования в импорте и использовать полное имя другого пользователя:
import my.own.Date;
class Test{
public static void main(String[] args){
// I want to choose my.own.Date here. How?
//Answer:
Date ownDate = new Date();
// I want to choose util.Date here. How ?
//Answer:
java.util.Date utilDate = new java.util.Date();
}
}
2 - всегда используйте полное имя класса:
//no Date import
class Test{
public static void main(String[] args){
// I want to choose my.own.Date here. How?
//Answer:
my.own.Date ownDate = new my.own.Date();
// I want to choose util.Date here. How ?
//Answer:
java.util.Date utilDate = new java.util.Date();
}
}
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 . Это может быть именно то, что вам нужно! :)
Я бы, наверное, пошел с чем-то вроде:
//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];
}
}
Заголовок вопроса и вопрос не полностью совпадают (заголовок запрашивает префикс, вопрос запрашивает суффикс / частичный)
Объедините следующие два селектора 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 с осторожностью!
Этот простой фрагмент мы используем на наших сайтах:
/**
* 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;
});
});
Если вы хотите найти что-то, заканчивающееся на '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