简体   繁体   English

执行JavaScript时,CSS规则无效。

[英]The CSS rule doesn't applies any effect when JavaScript is executed

I am a newbie in web, now I'm studying JS. 我是网络新手,现在正在学习JS。 I have this simple script to be executed: 我有一个简单的脚本要执行:

<script language="javascript">
var getDate = function() {
    var doc = document.getElementById("demo");
    doc.innerHTML = Date();
}
</script>

And this CSS rule for the label in which the script will be shown: 这是显示脚本的标签的CSS规则:

<style type="text/css">
.cstyle {
    font-family: "Comic Sans MS", cursive;
}
</style>

In my document I have a button that triggers my script 在我的文档中,有一个触发脚本的按钮

<p id="demo"><span class="cstyle">Result.</span></p>
<button type="button" onClick="getDate()">Display Data</button>

When the document is being loaded "Result" is displayed with "Comic Sans" font. 加载文档时,“结果”以“ Comic Sans”字体显示。 But when the mouse click has been happened, the date is displayed with the default font. 但是,当单击鼠标时,将以默认字体显示日期。 This behaviour is abnormal, because script execution result is needed to be displayed with "Comic Sans" font too. 此行为异常,因为脚本执行结果也需要使用“ Comic Sans”字体显示。 How can I fix this? 我怎样才能解决这个问题?


[Upd.] Sorry for a little mistake, this is doc.innerHTML = Date(); [更新]抱歉,有一个小错误,这是doc.innerHTML = Date(); instead of doc.innerHTML = arr.toString(); 而不是doc.innerHTML = arr.toString();

Your style is on the <span> element. 您的样式位于<span>元素上。 When you use change the innerHTML, the <span> element is replaced by the text. 当使用更改innerHTML时, <span>元素将替换为文本。 You need to update your code to be the following: 您需要将代码更新为以下内容:

HTML: HTML:

<p id="demo"><span id="result" class="cstyle">Result.</span></p>

and JS: 和JS:

var getDate = function() {
    var doc = document.getElementById("result");
    doc.innerHTML = Date();
}

This is because you are applying the font-family: "Comic Sans MS", cursive on the element with class .cstyle . 这是因为您正在对.cstyle类的元素应用font-family: "Comic Sans MS", cursive When clicking on the button, you are replacing the 点击按钮时,您将替换

<span class="cstyle">Result.</span>

with a string value (without the . cstyle class which provides the styling of your desired font). 带有字符串值(不提供提供所需字体样式的cstyle类)。

Just move your class .cstyle on the parent element <p> . 只需将类.cstyle移动到父元素<p>

<p id="demo" class="cstyle"><span >Result.</span></p>
<button type="button" onClick="getDate()">Display Data</button>

You can even leave the <span> element, which is not needed in this case. 您甚至可以保留<span>元素,这种情况下不需要。

<p id="demo" class="cstyle">Result.</p>
<button type="button" onClick="getDate()">Display Data</button>

Unfortunately you are replacing the span with the cstyle with just the date. 不幸的是,您只用日期替换了cstyle。 Remember you have selected the p with id demo to insert your result so you simply replace its html using innerHTML. 记住,您已经选择了带有id demo的p来插入结果,因此您只需使用innerHTML替换其html。 If that paragraph had also the class cstyle then everything would be fine. 如果该段也具有cstyle类,那么一切都会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 执行JavaScript代码时CSS样式不适用 - CSS style doesn't apply when javascript code is executed 新的JavaScript HTML元素未遵循CSS规则 - New javascript HTML element doesn't follows css rule Javascript:将代码放入函数后不会执行 - Javascript: Code doesn't get executed when put into function 我的 javascript 代码不起作用,但在控制台中执行时它起作用 - My javascript code doesn't work, but when it is executed in the console it works 为什么setter方法对我的JavaScript对象中的其他变量没有任何影响? - Why doesn't the setter method have any effect on other variables in my JavaScript object? 隐藏 div 的 Javascript 代码不起作用(CSS @media 规则也不起作用)-“无法读取 null 的属性样式” - Javascript code hiding a div doesn't work (CSS @media rule doesn't work either)- 'cannot read property style of null' 可拖动效果在 Javascript 中不起作用 - Draggable effect doesn't work in Javascript Javascript悬停效果在iPad上不起作用 - Javascript hover effect doesn't work on iPad 从另一页的DIV中加载的页面执行时,javaScript onClick不起作用 - javaScript onClick doesn't work when executed from page loaded inside a DIV on another page 当我从链接访问页面时,Javascript第一次没有执行 - Javascript doesn't get executed the first time when I access a page from a link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM