简体   繁体   English

背景图像:url() 在打字稿中不起作用?

[英]background-image : url() not working in typescript?

In my rails project I try to hide and show an image when a user click on it, here is my code :在我的 rails 项目中,我尝试在用户单击时隐藏和显示图像,这是我的代码:

$(document).ready(function() {
  if ($("#myvideo").prop('muted', true)){
    $("#mute").css("background-image","asset_url(mute.svg)");
  }

$("#mute").click( function (){
  if( $("#myvideo").prop('muted') ) {
    $("#myvideo").prop('muted', false);
    $("#mute").css("background-image","url(app/assets/icons/speaker.svg)");
  } else {
    $("#myvideo").prop('muted', true);
    $("#mute").css("background-image","url('mute.svg')");
  }
});
});

none of this try works but when I put an url like $("#mute").css("background-image","url(https://www.svgrepo.com/show/170684/mute-volume-control.svg)");这些尝试都$("#mute").css("background-image","url(https://www.svgrepo.com/show/170684/mute-volume-control.svg)");但是当我放置一个像$("#mute").css("background-image","url(https://www.svgrepo.com/show/170684/mute-volume-control.svg)"); it's ok,没关系,

Edit : also tried :编辑:也试过:

$("#mute").css("background-image","url(<%= asset_path 'mute.svg' %>)");

but it doesn't work但它不起作用

Anyone knows why ?有谁知道为什么? And how to fix it ?以及如何修复它?

It is not simple as you think.这并不像你想的那么简单。

You need load images from server with asset_path .您需要使用asset_path从服务器加载图像。

See docs here https://guides.rubyonrails.org/asset_pipeline.html在此处查看文档https://guides.rubyonrails.org/asset_pipeline.html

If this works for you thats mean you've problem in your image path如果这对你有用,那意味着你的图像路径有问题

$("#mute").css("background-image","url(https://www.svgrepo.com/show/170684/mute-volume-control.svg)");

there two ways: get your app path by asset_path then有两种方法:通过asset_path获取您的应用程序路径然后

$("#mute").css("background-image","url("+YOUR_ASSET_PATH+"assets/icons/speaker.svg)");

OR try或尝试

$("#mute").css("background-image","url(/assets/icons/speaker.svg)");

app/assets/images文件夹中移动/复制 mute.svg,然后使用下面的代码访问它

$("#mute").css("background-image","url('/assets/mute.svg')"

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

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