通过CSS注入HTML [英] Injecting HTML via CSS

查看:104
本文介绍了通过CSS注入HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上需要使用CSS中的HTML设置内容。我目前正在执行以下操作:

I need to basically set the content of something with HTML from CSS. I'm currently doing the following:

.myclass {
    content "<img src=\"hello.png\"/>";
}

但是,我看到的是文字,而不是图片:

However, instead of the image, I see the literal text:

<img src="hello.png"/>

如何使用CSS注入任意HTML?

How can I inject arbitrary HTML using CSS?

推荐答案

HTML存储数据,Model

CSS存储样式,View

JS存储交互,并且是控制器

HTML stores the data, and is the Model
CSS stores the styles, and is the View
JS stores the interactions, and is the Controller

如果您要向页面添加数据,则应通过HTML完成。如果您只是想将图像添加为样式,请使用 background-image 属性。您无需在页面中插入< img> 元素即可。

If you're trying to add data to the page, it should be done via HTML. If you're simply trying to add an image as a style, use the background-image property. You don't need to inject an <img> element in the page to do that.

就无法通过CSS将HTML注入页面而言,这是不可能的,但是可以添加使用CSS将JavaScript插入页面,然后可以将HTML添加到页面中。

As far as being able to inject HTML into the page via CSS, it's not directly possible, however it's possible to add JavaScript into the page using CSS, which can then add HTML to the page.

我不太强调这种方法的错误程度。

I can't emphasize enough how wrong that approach would be.

这篇关于通过CSS注入HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆