简体   繁体   English

如何使用 css 或 javascript 从 div 中删除 url

[英]How to remove url from div using css or javascript

如何从后台删除或替换 www.exaple.com/logos/rs-b.png: url using css or java script

<div style="width:17px;height: 15px;background: url(//www.exaple.com/logos/rs-b.png) no-repeat right;background-size: contain;z-index: 1000;bottom: 1px;right: 1px;position: fixed;">

Without external libraries, you have two options.如果没有外部库,您有两种选择。 Both of these require you to add an ID or a class in order to identify the div.这两者都需要您添加 ID 或类以识别 div。 I'll use an ID:我将使用一个 ID:

<div id="custom-id" style="background: url('https://example.com')"></div>

1) Override the inline style with a CSS !important declaration: 1) 使用 CSS !important声明覆盖内联样式:

You can add the following snippet in a <style> element or in an external stylesheet:您可以在<style>元素或外部样式表中添加以下代码段:

<style>
#custom-id {
    background: url('https://new-url.org') !important;
}
</style>

2) Use JavaScript to manipulate the inline style of the element: 2)使用JavaScript来操作元素的内联样式:

You can do so with the built in DOM API of the browser, like so:您可以使用浏览器的内置 DOM API 来实现,如下所示:

<script>
let div = document.getElementById('custom-id');
div.style.background = "url('https://new-url.org')";
</script>

EDIT编辑

After realizing you can't modify the HTML I came up with the following solution (that I already mentioned in the comments): Use the style attribute to identify the div.在意识到你不能修改 HTML 之后,我想出了以下解决方案(我已经在评论中提到过):使用 style 属性来标识 div。 The answer above is still correct but with the exception that the CSS selector should change to match the style attribute:上面的答案仍然是正确的,但 CSS 选择器应该更改以匹配样式属性:

div[style="width:17px;height: 15px;background: url(//www.exapl..."] {
    background: url('https://new-url.org') !important;
}

And the JS code too:还有 JS 代码:

let div = document.querySelector('div[style="width:17px;height: 15px;background: url(//www.exapl..."]');
div.style.background = "url('https://new-url.org')";

While the best solution is to change the HTML, if you can't do that I think this is an acceptable solution that solves the problem.虽然最好的解决方案是更改 HTML,但如果您不能这样做,我认为这是解决问题的可接受的解决方案。

使用它的属性查找并修改样式值

$('div.[style*="background:your_url"]).css('background', 'NewURL');

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

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