html - 更改特定选定选项的样式
问题描述
我正在尝试使用 select 生成短语的代码,您可以对其进行编辑以更改短语的含义。我想将“est”这个词添加到一个类中,以便它以与其他词不同的字体显示在页面中。.un
实际上,每次有一个单词时,我的代码都会重复 div 。
我如何告诉代码是否option
适用class ="something"
于其特定的.un
另一种样式?
我不想编辑列表中的样式(因为如您所见,它已经完成),我想更改显示结果的样式。 我希望以与列表中相同的样式显示“est”一词。
这是我的代码:
$(document).ready(function() {
});
let phrases = ["il n'est pas. "
];
let lettres = /[\u0041-\u005a\u0061-\u007a\u00aa-\u00b5\u00ba-\u00c0\u00c1-\u00d6\u00d8-\u00f6]/gui;
tokenize = function(phrase) {
let mots = [''];
let it = phrase[Symbol.iterator]();
for (var c = it.next(); !c.done; ) {
for (; !c.done && c.value.match(lettres); c = it.next()) {
mots[mots.length-1]+=(c.value);
}
//console.log(c.value);
if (mots[mots.length-1].length != 0 ){
mots.push('');
}
if (c.value == ' ') {
for (c = it.next(); !c.done && c.value == " "; c = it.next()) {continue;} continue;
}
// console.log(i);
console.log(mots);
if (!c.value.match(lettres)){
mots[mots.length-1]+=(c.value);
//console.log(c.value);
mots.push('');
}
c = it.next();
}
return mots.slice(0, mots.length-1);
}
$(document).ready(function() {
let LARGEUR = $("#container .repeat").clone();
let change=function(){
$(".width_tmp_option", this.parent).html($('option:selected', this).val());
$(this).width($(".width_tmp_select", this.parent).width());
}
$('#container').on("change",".un",change);
let idx = Math.floor(Math.random() * Math.floor(phrases.length));
let mots = tokenize(phrases[idx]);
for( var i = 0 ; i < mots.length-1; i++){
$('#container .repeat:last-child').after(LARGEUR.clone());}
var i = 0;
console.log(mots.length);
$('#container .repeat').each(function(){
$('.un', this).val(mots[i]).each(change);
i++;
});
$('select').on('change', function() {
let phrase = $('.un').get().reduce((a,b) => a+' '+b.value, '').substring(1).trim();
console.log(phrase);
if (phrase.includes('il est bleu')) {
$(".white").css('background-color', 'blue');
} else {
$(".white").css('background-color', 'white');
}
});
});
.something{
color:red;
font-family: cursive;
}
body {
width: 70vw;
height: 100vh;
overflow: normal;
overflow: hidden;
}
.un{
width: 2rem;
margin: 0.2rem;
font-family: sans-serif;
font-size: 2.5rem;
}
.un * {
border-radius: 15px;
font-size: 2.5rem;
}
option {
background-color: none;
}
option:hover {
background-color: green;
}
::selection {
background-color: green;
}
.width_tmp_select{
background-color: none;
}
.width_tmp_select{
display : none;
font-size: 2.5rem;
font-family: 'Rungli-Italic';
}
#p1{
font-size: 2rem;
border: none;
}
/* For IE10 */
select::-ms-expand {
display: none;
}
.repeat {
display: inline-block;
}
.continuer {
opacity: 0.2;
position: absolute;
bottom: 0.5vw;
right: 0.5vw;
font-size: 2.5rem;
padding: 0.1vw;
}
.continuer:hover {
opacity: 1;
background-color: white;
}
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<body class="white">
<div id="container">
<div class="repeat">
<select name="mots" class="un">
<option value="il">il</option>
<option class="something" value="est">est</option> // I would like this one to have another style that the rest of the phrase.
<option value="bleu">bleu</option>
<option value="n">n</option>
<option value="'">'</option>
<option value="pas">pas</option>
<option value=".">.</option>
</select>
<select class="width_tmp_select">
<option class="width_tmp_option"></option>
</select>
</div>
</div>
</body>
解决方案
您可以在 $('select').change 函数中检查所选选项的类
$('select').on('change', function () {
let phrase = $('.un').get().reduce((a, b) => a + ' ' + b.value, '').substring(1).trim();
console.log(phrase);
var selectedOption = $(this).find('option:selected');
var optionClass = selectedOption.attr('class');//here you can get option class
if(optionClass === "something"){
//do stuff here
}
if (phrase.includes('il est bleu')) {
$(".white").css('background-color', 'blue');
} else {
$(".white").css('background-color', 'white');
}
});
顺便说一句,您为所有下拉菜单应用了更改事件,因此$(this).find('option:selected') 将为您提供当前下拉菜单的当前选定选项
推荐阅读
- reactjs - 在 ES6 中重新声明一个标识符
- android - 从 android 服务中的 get SharedPreferences 中获取数据
- python - 将希腊语 HTML 字母转录到 LaTex 时出现问题
- javascript - 修改输入值后如何复制表行并追加
- javascript - 如何在另一个动态div中的动态li中获取span的第一个值
- c# - 使用谷歌地图 api 密钥的 C# Rest 服务在本地工作,但不在服务器上
- ruby-on-rails - Rails 中无限时间范围的问题
- python - 找到一个变量的最大值和最小值并找到对应的值
- r - 如何在R中的同一数据框中合并2列
- android - 如何从另一个类更新片段的用户界面?