[英]Is there a pseudo-class selector for javascript modified nodes?
function myfunc(){ document.getElementById("bob").innerText += "azerty"; }
#bob{ color: green; transition: all 1s; } #bob:modified{ /* ":modified" pseudo-class doesn't exists; I'm searching for the good one */ color: red; }
<div>I would like the green text below to turn red when it changes and then green again.</div> <div id="bob">azerty</div> <button onclick="myfunc();">click to modify</button>
I want to create a transition when I modify the text of my node via javascript. Is there a selector for nodes whose content is modified?我想在通过 javascript 修改我的节点的文本时创建一个转换。是否有用于修改内容的节点的选择器?
I tried ":first-child" by deleting then recreating the node, and ":defined", but it did not work.我通过删除然后重新创建节点来尝试“:first-child”和“:defined”,但它没有用。
I would like the transition to apply when the text is changed.我希望在更改文本时应用过渡。
as you use js an idea can be to add an attribute or a class to your element当你使用 js 时,一个想法可以是向你的元素添加一个属性或 class
bob.dataset.modified = true;
bob.classList.add('modified');
and use css selector to pass new style并使用 css 选择器传递新样式
function myfuncClass() { const bob = document.getElementById("bob"); bob.innerText += "azerty"; bob.classList.add('modified'); } function myfunc() { const bob = document.getElementById("bob"); bob.innerText += "azerty"; bob.dataset.modified = true; }
#bob { color: green; transition: all 1s; } #bob[data-modified] { color: red; } #bob.modified { color: blue; }
<div>I would like the green text below to turn red when it changes and then green again.</div> <div id="bob">azerty</div> <button onclick="myfuncClass();">click to modify by adding class</button> <button onclick="myfunc();">click to modify by add an attribute</button>
I would use the Web Animations API for this, instead of CSS transitions:我会为此使用Web 动画 API ,而不是 CSS 过渡:
const el = document.getElementById("bob"); const [modifiedAnimation] = el.getAnimations(); modifiedAnimation.pause(); // don't run immediately function myfunc(){ el.innerText += "azerty"; modifiedAnimation.play(); }
#bob { color: green; animation: modified 1s; } @keyframes modified { 50% { color: red; } }
<div>I would like the green text below to turn red when it changes and then green again.</div> <div id="bob">azerty</div> <button onclick="myfunc();">click to modify</button>
Above is a mix of CSS animations with JS, you can also do it entirely in JavaScript by using the animate
method .上面是 CSS 动画和 JS 的混合,你也可以用
animate
方法完全在 JavaScript 中完成。
Thanks to jeremy-denis's answer, I found out how to get the expected behaviour.感谢 jeremy-denis 的回答,我发现了如何获得预期的行为。
function myfunc(){ let bob = document.getElementById("bob") bob.innerText += "azerty"; bob.classList.add('modified'); setTimeout(function() { bob.classList.remove('modified'); }, 1000); }
#bob{ color: green; transition: all 1s; } #bob.modified{ color: red; }
<div>I would like the green text below to turn red when it changes and then green again.</div> <div id="bob">azerty</div> <button onclick="myfunc();">click to modify</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.