我在设计简单的 html 样式时遇到了各种麻烦 在 js 和 jQuery 中 这里好像用了一个jsfiddle来说明。 我是新手,看不到控制台输出或期望的结果 所以我问这个问题是为了了解更多关于如何提供帮助的信息; 我确实搜索了 .css not working and variations
JavaScript
.css() is jQuery. .style is js.
document.getElementsByTagName('pre').text.css("color: blue;"); jquery: const preSentences = $("pre").text().split(".") console.log({preSentences})
//no err no workee in chrome
//preSentences.forEach(function(str) { $(this).css("color: blue; border: 1px solid black")}); //$.each(preSentences, function(str) { $(this).blue }) //preSentences[1].style("color: blue; border: 1px solid black");
我认为 fiddle 在这里 - 我尝试了嵌入链接但它没有显示: https://jsfiddle.net/Lz8nh50o/
最佳答案
getElementsByTagName 将返回一个标签数组,您必须遍历每个标签来设置样式。此外,您正在使用 .css() 但这是一个 jQuery 函数。你必须使用风格。
试试这个:
var preTags = document.getElementsByTagName('pre');
for(var i=0; i<preTags.length; i++){
preTags[i].style.color='blue';
}
关于Javascript .css 不工作也不 jquery .blue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244446/