简体   繁体   English

更改 <p> 使用纯JavaScript基于文本长度的CSS样式

[英]Change <p> css style based on text length using plain javaScript

I would like to style a <p> dependant on the amount of text it contains ; 我想根据它包含的文本量来设置<p>样式;
using plain - vanila javaScript {no external library} 使用纯文本-vanila javaScript {无外部库}
+ of course css & html 4.1 for use in pre 29 FireFox. + 当然还有用于29之前的FireFox的css和html 4.1。
Using 1 : Change an element's class with JavaScript - for the way to reference the html element getElementById("MyElement") ... 使用1: 使用JavaScript更改元素的类 -引用html元素getElementById("MyElement") ...
and adapting : How to detect when multiple lines are occurring in a div? 和适应: 如何检测div中何时出现多行? ele.innerHTML.length >= 70 to my specific needs. ele.innerHTML.length >= 70满足我的特定需求。
I cannot get http://jsfiddle.net/positive_x/HeCj7/ to work. 我无法使http://jsfiddle.net/positive_x/HeCj7/正常工作。
In that jsfiddle , I am trying to use the first way to prove - out & learn the algorithm ; 在那个jsfiddle中,我试图用第一种方式证明和学习算法;
html + css & javaScript in the jsfiddle . jsfiddle中的html + css和javaScript net / positive_x / HeCj7 / 净/ positive_x / HeCj7 /

<aside>
    <p>This is a plain - old paragraph.</p>
    <p id='p_id1'>This is the p_id1 (blue) p</p>
    <p id='p_id2'>This is the p_id2 (really l o n g ___ R E D ) paragraph . </p>
    <p class='styletest'>This is a short class= p</p>
    <p class='styletest'>This is other class= (really long _ R E D) paragraph .</p>
</aside>
<script>window.onload = style_byID_Blue_short_p;</script>

/* css in jsfiddle */ / * jsfiddle中的CSS * /

.blue {color: #117;}
.green {color: #161;}
.red  {color: #511;}

/* javascript in jsfiddle TESTing 1 getElementById('idname') way */ / * jsfiddle中的javascript测试1 getElementById('idname')方式* /

function style_byID_Blue_short_p () {
    var textIn = document.getElementById('p_id1');
    var txtLen = textIn.innerHTML.length;
    if( txtLen<30 ){
        textIn.className += ' blue'; //turn it blue
        alert('here is style_byID_Blue_short_p that IDS blue!'); //for testing
    } else {
        textIn.style.color = #511; //turn it red
        alert('style_byID_Blue_short_p that is red.'); //for testing
    }
}


Additionally, I really need to apply the styling to a number of <p> 另外,我确实需要将样式应用于多个<p>
via the getElementsByClass("myclassname") that is in the second part of the jsfiddle shown in the jsfiddle itself. 经由getElementsByClass("myclassname")是在第二部分的jsfiddle在的jsfiddle本身所示。
The required array nomenclature has me intimidated ; 要求的数组命名法使我感到害怕; (
; ; so I attempted the first _ simpler portion first . 所以我第一次尝试第一_简单的部分。
As a copy-&-paste coder , I could use your help. 作为copy-&-paste ,我可以使用您的帮助。
Best regards. 最好的祝福。

Well, besides the tons of syntax and spelling errors and not placing CSS colors in quotes (you should really use the browser console and JSHint on jsfiddle), your problem has to do with the fact that your handlers aren't applied properly. 好吧,除了大量的语法和拼写错误并且没有在引号中放置CSS颜色(您应该真正在jsfiddle上使用浏览器控制台和JSHint),您的问题还与处理程序使用不正确有关。

You need to use addEventHandler to make sure the event handlers are attached after your javascript is parsed. 您需要使用addEventHandler来确保在解析JavaScript后附加了事件处理程序。 If you had checked the console, you'd have seen that they are undefined at load time, hence nothing runs. 如果检查了控制台,您会发现它们在加载时是未定义的,因此没有任何运行。

Instead of: 代替:

<script>
    window.onload = style_byClass_RED_Long;
</script>

do

window.addEventListener('onload', style_byClass_RED_Long);

and put that in your javascript. 并将其放在您的JavaScript中。 However, that might not work in a fiddle, so just call your functions at the bottom of your javascript and it should produce your results. 但是,这可能不起作用,因此只需在javascript底部调用函数,它就会产生结果。 For all practical purposes, it should be the same thing as using onload . 出于所有实际目的,它应该与使用onload相同。

here's your fiddle fixed: http://jsfiddle.net/HeCj7/9/ 这是您固定的小提琴: http : //jsfiddle.net/HeCj7/9/

also, getElementsByClassName returns an array of elements, not a single one. 同样, getElementsByClassName返回一个元素array ,而不是单个元素。 You'll need to enumerate the elements and change each one separately. 您需要枚举元素,然后分别更改每个元素。 In your code, i simply changed it so that it refers to the first element only, which means your second <p> with that class will remain unstyled. 在您的代码中,我只是对其进行了更改,以使其仅引用第一个元素,这意味着您使用该类的第二个<p>将保持未样式化。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM