简体   繁体   English

如何使用JavaScript查找被单击的元素(或具有背景图像的最近的子元素或父元素)的背景图像?

[英]How can I find the background image of a clicked element (or the nearest child or parent element that has a background image) using JavaScript?

I'm writing a Chrome extension that I want to use to provide the user with the URL of a background image using Chrome's right-click context menu. 我正在编写一个Chrome扩展程序,该程序可用于使用Chrome的右键单击上下文菜单为用户提供背景图片的URL。

Right now my problem is finding the background image. 现在我的问题是找到背景图像。 If a user right-clicks the correct element then it's easy. 如果用户右键单击正确的元素,那么这很容易。 But if they click a child of that element then I have to look at the parents. 但是,如果他们单击该元素的一个孩子,那么我必须看看父母。 What's the most efficient way to handle this? 处理此问题的最有效方法是什么?

Some notes: I'm not injecting jQuery with my extension. 一些注意事项:我没有使用扩展名注入jQuery。 And I've also thought of the scenario where an element that is absolutely positioned above the desired background image would get in the way. 而且我还想到了这样一种情况,即绝对位于所需背景图像上方的元素会挡住。 But I'm not concerned with that at the moment. 但是我现在不关心这个。

Here is my code so far. 到目前为止,这是我的代码。 It works as long as the clicked element has a background-image . 只要clicked元素具有background-image它就可以工作。 Failing to find one, it does not look at the parents yet. 没有找到一个,它还没有看着父母。

document.body.addEventListener('contextmenu', function(ev) {

    // get computed style
    var style = window.getComputedStyle(ev.target, false);

    // img src is inside 'url(' + ')' - slice those off
    var src = style.backgroundImage.slice(4, -1);

    console.log(src);

    return false;
}, false);

 function getBackgroundImage(el) { if (el == null) { return null; } var backgroundImage = window.getComputedStyle(el, false).getPropertyValue("background-image"); if (backgroundImage !== 'none') { return backgroundImage.slice(4, -1); } else { return getBackgroundImage(el.parentElement); } } document.body.addEventListener('contextmenu', function(ev) { var src = getBackgroundImage(ev.target); console.log(src); }, false); 
 .grand-child { height: 100px; width: 100px; background: green; } .parent { background: url('http://google.com'); } 
 <div class="parent"> <div class="child"> <div class="grand-child"> </div> </div> </div> 

暂无
暂无

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

相关问题 如何将 Javascript 元素放入 CSS 背景图像 - How can I put Javascript element into CSS background-image 如何使用JavaScript将一个图像元素设置为另一个div的背景图像 - How can I set one image element as background image of another div using javascript 如何在 JavaScript 中检查 div 是否包含图像元素? (不像背景图片,像包含的元素) - How can I check to see if a div contains an image element in JavaScript? (Not like a background image, like the element contained) 更改元素的背景图像以匹配单击元素的背景图像 - Changing background image of element to match that of clicked element 在jQuery中,如何在不使用JavaScript对图像的URL进行硬编码的情况下设置元素的背景图像? - In jquery, how can I set the background-image of an element without hardcoding the url of the image in my javascript? 如何使用javascript在html中获取元素背景图像 - How to get the element background image in html using javascript 如何使用 JavaScript 获取元素的背景图片 URL? - How to get background image URL of an element using JavaScript? 如何使用CSS动画/ JavaScript使具有背景图像的元素出现 - How to make an element with background image appear using css animation/javascript 如何将图像的一部分从父元素传输到子元素 - How can I transfer part of an image from a parent element to a child 如何在父div中使用Jquery而不影响子div的情况下淡入和淡出背景图像? - How can I fade are background image in and out using Jquery in a parent div without having it affect the child div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM