Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 我可以在Rails视图中指定css::before选择器样式吗?_Jquery_Html_Css_Ruby On Rails - Fatal编程技术网

Jquery 我可以在Rails视图中指定css::before选择器样式吗?

Jquery 我可以在Rails视图中指定css::before选择器样式吗?,jquery,html,css,ruby-on-rails,Jquery,Html,Css,Ruby On Rails,在我的css中,我有以下内容: .inner-class { content: ''; } .inner-class:before { background: url(http://www.fillmurray.com/500/500.jpg); content: ''; } <div class="outer-class"> <div class="inner-class"> ::before </div> </div&

在我的css中,我有以下内容:

.inner-class {
  content: '';
}

.inner-class:before {
  background: url(http://www.fillmurray.com/500/500.jpg);
  content: '';
}
<div class="outer-class">
  <div class="inner-class">
    ::before
  </div>
</div>
在我的视图的html.erb中,我有以下内容:

.inner-class {
  content: '';
}

.inner-class:before {
  background: url(http://www.fillmurray.com/500/500.jpg);
  content: '';
}
<div class="outer-class">
  <div class="inner-class">
    ::before
  </div>
</div>

::之前
我想使用Rails更改::before选择器的属性。通常,我会这样做:

<div class="outer-class">
  <div class="inner-class" style:"background: url(<% image_path %>)">
    ::before
  </div>
</div>

::之前

但是,这不会更改内部类的属性:before。如何做到这一点?

您应该做的是添加一个额外的CSS类,并更改::before pseudo,然后有条件地将该类应用于父元素

  • 从ERB中删除
    ::before
    文本
  • 添加一个CSS类,如下所示:
  • 向雇员再培训局添加条件,如下所示:

  • 我不确定这在这种特殊情况下是否对我有帮助。我想在视图中动态生成图像,而不是在css中设置它。因此,类似于
    的内容将被设置为::before元素中的背景图像。我想在阅读了您的评论后,我已经决定采用另一种显示图像的方式,但我想我会尝试您的方法,看看是否可以使其正常工作。这样尝试会得到一个“未初始化常量ActiveSupport::Base64”“错误。
    是否需要'base64'
    ?在使用ActiveSupport::base64的任何文件的顶部。如果在本例中是'article.rb',则是。”。对不起,我不太熟悉它的工作原理。您不能在线执行。但可以将样式标记附加到头部
    <div class='inner-class' data-image="<% ActiveSupport::Base64.encode64(open(article.user.avatar_url) { |io| io.read }) %>"></div>
    
     .inner-class:before { 
       content: url(attr(data-image)); 
       height: 40px; 
       width: 40px;  /* Whatever the expected height and width of the image are */`
     }