Можно ли использовать ":: - webkit-input-placeholder" с jQuery для установки цвета для текста заполнителя?
Что-то вроде этого:
$("input::-webkit-input-placeholder").css({"color" : "#b2cde0"});
Можно ли использовать ":: - webkit-input-placeholder" с jQuery для установки цвета для текста заполнителя?
Что-то вроде этого:
$("input::-webkit-input-placeholder").css({"color" : "#b2cde0"});
Вы не можете модифицировать псевдоселекторов с помощью JavaScript. Вам придется изменить существующий элемент <style>
.
Если возможно, создайте класс:
.your-class::-webkit-input-placeholder {
color: #b2cde0
}
И добавьте его в элемент:
$('input').addClass('your-class');
Я использовал MaterializeCSS; Я использовал Jquery для обновления CSS для полей ввода, таких как
$(".input-field").css("color", themeColor);
$(".input-field>.material-icons").css("color", themeColor);
$(".input-field>label").css("color", themeColor);
См. результат:
Вот пример динамической установки стилей псевдоэлементов с помощью jQuery - это вопрос создания элемента <style>
, установки его текстового содержимого в требуемые декларации стиля и добавления его в документ.
Вот простой пример с одной страницей:
<!doctype html>
<html>
<head>
<title>Dynamic Pseudo-element Styles</title>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script>
$(document).ready(function() {
createStyles();
$('#slider-font-size').on('change', createStyles);
function createStyles() {
// remove previous styles
$('#ph-styles').remove();
// create a new <style> element, set its ID
var $style = $('<style>').attr('id', 'ph-styles');
// get the value of the font-size control
var fontSize = parseInt($('#slider-font-size').val(), 10);
// create the style string: it the text node
// of our <style> element
$style.text(
'::placeholder { ' +
'font-family: "Times New Roman", serif;' +
'font-size: ' + fontSize + 'px;' +
'}');
// append it to the <head> of our document
$style.appendTo('head');
}
});
</script>
</head>
<body>
<form>
<!-- uses the ::placeholder pseudo-element style in modern Chrome/Firefox -->
<input type="text" placeholder="Placeholder text..."><br>
<!-- add a bit of dynamism: set the placeholder font size -->
<input id="slider-font-size" type="range" min="10" max="24">
</form>
</body>
</html>