简体   繁体   English

使用条件css更改html文本但不使用js

[英]Change html Text with conditional css but without js

Sorry about this absolutely newbie question, but I've been searching for an already similar post, and couldn't find it. 抱歉这个绝对新手的问题,但我一直在寻找一个已经很相似的帖子,但找不到它。

My question is: 我的问题是:

I have a paragraph text in my HTML code which I want to automatically change into a new text once I click a specific Button . 我的HTML代码中有paragraph text ,我想在单击特定Button自动将其更改为新文本。

Can this be done with CSS only, without any javascript ? 这可以只使用CSS ,没有任何javascript吗?

Since some users block javascript, that's why I was looking for a way around... 由于一些用户阻止javascript,这就是我寻找方法的原因......

Thanks a lot. 非常感谢。

它无法通过CSS完成您必须为点击事件添加脚本。

I know it is not what you exactly want but it can give you idea about it and with some changes you can make it.(but conditional and on click event using css is definitely not possible, you need javascript for that) 我知道这不是你想要的,但它可以让你对它有所了解,并且你可以做一些改变。(但有条件的和使用css的点击事件肯定是不可能的,你需要javascript)

If you can make it work on Text click itself then it is easily possible. 如果您可以使用文本单击本身,那么很容易。 You only need a checkbox which is hidden and label in which you will show text. 您只需要一个隐藏的复选框和标签,您将在其中显示文本。 On click of text you can swap into anther text with only css: 点击文本,你可以只用css交换到花药文本:

  #example { position: relative; } #example-checkbox { display: none; } #example-checkbox:checked + #example:after { content: "Hide"; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: white; } 
  <input id="example-checkbox" type="checkbox"> <label for="example-checkbox" id="example">Show</label> 

Reference See css only part. 参考请参阅css only part。

Hope it will help you. 希望它会对你有所帮助。

actually @Vaidya & @Preet it is possible via css only : 实际上@Vaidya和@Preet只能通过css:

https://css-tricks.com/swapping-out-text-five-different-ways/ https://css-tricks.com/swapping-out-text-five-different-ways/

@fana you'll need to use a plus selector or tilde selector to make the changes affect the following div not itself but other than that you're good to go. @fana你需要使用 号选择 波形符号选择器来使更改影响下面的div本身,但除此之外你还好。

I can see that using css to replace content is strongly discouraged within the stackoverflow community. 我可以看到在stackoverflow社区中强烈建议不要使用css来替换内容。 However I haven't found another cause other then that of code cleanliness. 但是,除了代码清洁之外,我还没有找到其他原因。

I really think in coming years the true potential of CSS/SASS will unravel and people will cease to see the programmatic/dynamic as strictly excluded from CSS/SASS. 我真的认为在未来几年CSS / SASS的真正潜力将会解体,人们将不再看到CSS / SASS严格排除的程序/动态。

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

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