div输入的内容全选css,将CSS类应用于内容中的选定文本可编辑div

当您单击虚拟按钮,然后选择不见了。你需要这种方式绑定到mousedown事件:

function addAnimation() {

selectedElement = window.getSelection().focusNode.parentNode;

$(selectedElement).addClass('grad');

}

$('.animate-selected').on('mousedown', addAnimation);

.text-field {

background: #333;

color: #fff;

width: 300px;

height: 100px;

}

.grad {

-webkit-animation: changeColor 8s ease-in infinite;

animation: changeColor 8s ease-in infinite;

}

.animate-selected{

margin: 2px 0;

border: 1px solid blue;

display: inline-block;

padding: 0 4px;

cursor: pointer;

}

@-webkit-keyframes changeColor {

0% {

color: #ff7473;

}

25% {

color: #ffc952;

}

50% {

color: #fc913a

}

75% {

color: #75D701;

}

100% {

color: #ff7473

}

}

Hello world. Select this text and press the button

Animate

如果你想动画只选定的文本,比你做这种方式:

function addAnimation() {

$('.grad').contents().unwrap(); /* this removes previous animation */

var selectedText = window.getSelection();

var container = $(selectedText.anchorNode.parentNode);

var wrappedText = '' + selectedText + ''

container.html(container.html().replace(selectedText, wrappedText));

}

$('.animate-selected').on('mousedown', function(e) {

addAnimation();

});

.text-field {

background: #333;

color: #fff;

width: 300px;

height: 100px;

}

.grad {

-webkit-animation: changeColor 8s ease-in infinite;

animation: changeColor 8s ease-in infinite;

}

.animate-selected{

margin: 2px 0;

border: 1px solid blue;

display: inline-block;

padding: 0 4px;

cursor: pointer;

}

@-webkit-keyframes changeColor {

0% {

color: #ff7473;

}

25% {

color: #ffc952;

}

50% {

color: #fc913a

}

75% {

color: #75D701;

}

100% {

color: #ff7473

}

}

Hello world. Select this text and press the button

Animate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个实现勾选穿梭、全选、选项置灰和计数的 JavaScript 代码: HTML: ```html <div> <p>可选项目:</p> <ul id="select"> <li> <label> <input type="checkbox" value="1"> 选项1 </label> </li> <li> <label> <input type="checkbox" value="2"> 选项2 </label> </li> <li> <label> <input type="checkbox" value="3"> 选项3 </label> </li> <li> <label> <input type="checkbox" value="4"> 选项4 </label> </li> </ul> </div> <div> <button id="selectAll">全选</button> <button id="transfer">穿梭</button> </div> <div> <p>已选项目:</p> <ul id="selected"> </ul> </div> ``` CSS: ```css li.disabled { color: #ccc; pointer-events: none; } ``` JavaScript: ```javascript const select = document.getElementById('select'); const selected = document.getElementById('selected'); const selectAllBtn = document.getElementById('selectAll'); const transferBtn = document.getElementById('transfer'); function updateCount() { const selectedCount = selected.querySelectorAll('input[type="checkbox"]').length; const totalCount = select.querySelectorAll('input[type="checkbox"]').length; const countEl = document.getElementById('count'); countEl.innerHTML = `${selectedCount} / ${totalCount}`; } // 给所有的可选项目添加事件监听器 select.querySelectorAll('input[type="checkbox"]').forEach((checkbox) => { checkbox.addEventListener('change', () => { // 如果该项目已经被选,则将其添加到已选项目列表 if (checkbox.checked) { const li = document.createElement('li'); const label = document.createElement('label'); const text = document.createTextNode(checkbox.parentNode.innerText.trim()); const input = document.createElement('input'); input.type = 'checkbox'; input.value = checkbox.value; input.checked = true; input.addEventListener('change', () => { // 如果该项目被取消选,则将其从已选项目列表移除 if (!input.checked) { li.remove(); updateCount(); } }); label.appendChild(input); label.appendChild(text); li.appendChild(label); selected.appendChild(li); updateCount(); } // 如果该项目被取消选,则将其从已选项目列表移除 else { const li = selected.querySelector(`li input[value="${checkbox.value}"]`).parentNode.parentNode; li.remove(); updateCount(); } }); }); // 点击 "全选" 按钮时,将所有可选项目添加到已选项目列表 selectAllBtn.addEventListener('click', () => { select.querySelectorAll('input[type="checkbox"]').forEach((checkbox) => { checkbox.checked = true; checkbox.dispatchEvent(new Event('change')); }); }); // 点击 "穿梭" 按钮时,将所有已选项目移动到可选项目列表 transferBtn.addEventListener('click', () => { selected.querySelectorAll('input[type="checkbox"]').forEach((checkbox) => { const li = select.querySelector(`li input[value="${checkbox.value}"]`).parentNode.parentNode; li.classList.remove('disabled'); li.querySelector('input[type="checkbox"]').checked = false; li.querySelector('input[type="checkbox"]').dispatchEvent(new Event('change')); checkbox.parentNode.parentNode.remove(); }); updateCount(); }); // 当已选项目列表为空时,禁用 "穿梭" 按钮 const observer = new MutationObserver(() => { if (selected.children.length === 0) { transferBtn.disabled = true; } else { transferBtn.disabled = false; } }); observer.observe(selected, { childList: true }); ``` 实现的效果为: - 点击可选项目后,将其添加到已选项目列表; - 点击已选项目后,将其从已选项目列表移除; - 点击 "全选" 按钮后,将所有可选项目添加到已选项目列表; - 点击 "穿梭" 按钮后,将所有已选项目移动到可选项目列表,并将已选项目的复选框取消选; - 当已选项目列表为空时,禁用 "穿梭" 按钮; - 当已选项目列表的项目被移除时,更新计数器的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值