简体   繁体   English

用于突出显示 html 元素的纯 JavaScript 代码

[英]plain javascript code to highlight an html element

to debug some javascript code, I am looking for javascript code (preferably just js, without libraries and dependencies) that can highlight a div or span (probably by putting over it a div or span of the same size and shape with a bright color and some transparency).为了调试一些 javascript 代码,我正在寻找可以突出显示 div 或跨度的 javascript 代码(最好只是 js,没有库和依赖项)(可能通过将具有相同大小和形状的 div 或跨度放在上面,颜色鲜艳,并且一些透明度)。

I pretty sure it can be done, but I don't know how to start.我很确定它可以完成,但我不知道如何开始。

CLARIFICATION澄清

I need to put a semi transparent div on top of my element.我需要在我的元素顶部放置一个半透明的 div。 Modifying the background or adding borders will not help as my elements have themselves backgrounds and borders.修改背景或添加边框无济于事,因为我的元素有自己的背景和边框。

element.style.backgroundColor = "#FDFF47";

#FDFF47 is a nice shade of yellow that seems perfect for highlighting. #FDFF47 是一种很好的黄色阴影,看起来非常适合突出显示。

Edit for clarification: You're over-complicating things.编辑澄清:你把事情复杂化了。 If you ever want to restore the previous background color, just store element.style.backgroundColor and access it later.如果您想恢复以前的背景颜色,只需存储element.style.backgroundColor并稍后访问它。

如果您在支持 CSS outline的浏览器中进行调试,一个简单的解决方案是:

myElement.style.outline = '#f00 solid 2px';
function highlight(element) {
    var div = highlight.div; // only highlight one element per page

    if(element === null) { // remove highlight via `highlight(null)`
        if(div.parentNode) div.parentNode.removeChild(div);
        return;
    }

    var width = element.offsetWidth,
        height = element.offsetHeight;

    div.style.width = width + 'px';
    div.style.height = height + 'px';

    element.offsetParent.appendChild(div);

    div.style.left = element.offsetLeft + (width - div.offsetWidth) / 2 + 'px';
    div.style.top = element.offsetTop + (height - div.offsetHeight) / 2 + 'px';
}

highlight.div = document.createElement('div');

// set highlight styles
with(highlight.div.style) {
    position = 'absolute';
    border = '5px solid red';
}

Do you use Firebug?你用萤火虫吗? It makes it very simple to identify dom elements and will highlight them in the page as you walk through the dom.它使识别 dom 元素变得非常简单,并且会在您浏览 dom 时在页面中突出显示它们。

Old post, but worth adding since it shows up in searches on the topic.旧帖子,但值得添加,因为它出现在对该主题的搜索中。 A simple way to achieve a highlighting effect is:实现高亮效果的简单方法是:

myElement.style.filter = "brightness(125%)";

Here is a function that combines the top 2 answers:这是一个结合了前 2 个答案的函数:

 function highlight(element){ let defaultBG = element.style.backgroundColor; let defaultOutline = element.style.outline; element.style.backgroundColor = "#FDFF47"; element.style.outline = '#f00 solid 4px'; setTimeout(function() { element.style.backgroundColor = defaultBG; element.style.outline = defaultOutline; }, 2000); }


If for some reason you need to use javascript here is function that temporary highlits element background如果由于某种原因你需要使用 javascript,这里是临时高亮元素背景的函数

function highlight(element) {
    let defaultBG = element.style.backgroundColor;
    let defaultTransition = element.style.transition;

    element.style.transition = "background 1s";
    element.style.backgroundColor = "#FDFF47";

    setTimeout(function()
    {
        element.style.backgroundColor = defaultBG;
        setTimeout(function() {
            element.style.transition = defaultTransition;
        }, 1000);
    }, 1000);
}

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

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