为帖子中的图片添加 ID 或 CLASS

You can’t by default. There’s only a few attributes that are whitelisted for <img> tags. You can find those here:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/pretty-text/white-lister.js.es6#L170-L173

Whitelisting specific classes can be done but would require a plugin.

How often do you plan on using this? You might be able to make something work with a theme component but the implementation will depend on your use case. Can you share a specific example of what you’re trying to do?

for example, you can wrap the image in something like

<div data-theme-banners>
  <img src='foo'>
</div>

then you can use the selector

[data-theme-banners] img