简体   繁体   English

隐藏元素,但保持可点击状态?

[英]Hide element, but keep it clickable?

I have a button generated inside an iframe. 我在iframe中生成了一个按钮。 Unfortunately, I can't change how it looks, as it's delivered by 3rd party library. 不幸的是,我无法更改它的外观,因为它是3rd party库提供的。 I thought of a little trick to use my own button and keep the generated one inside: 我想到了一个小技巧,可以使用我自己的按钮并将生成的按钮保留在里面:

<button id="my-button">Click Me</button>

This way, I can tell the library to place its buttons inside mine, so the <iframe> would get appended like this: 这样,我可以告诉库将其按钮放置在我的按钮中,这样<iframe>就会像这样追加:

<button id="my-button">
    Click Me
    <iframe src="..."></iframe>
</button>

Now, the only thing left is to hide the <iframe> . 现在,剩下的唯一事情就是隐藏<iframe> I can't simply use visibility: hidden , because that way the click event no longer works. 我不能简单地使用visibility: hidden ,因为那样, click事件将不再起作用。 Why I did is instead: 我这样做的原因是:

#my-button {
    overflow: hidden;
    position: relative;
}

#my-button > * {
    position: absolute;
    top: 0;
    bottom: 0;
    opacity: .0001;
}

It seems to be a good solution, as I don't see the 3rd party button and I can do whatever I want with my own button. 这似乎是一个很好的解决方案,因为我看不到“第三方”按钮,并且可以使用自己的按钮进行任何操作。 I just need to make sure it's not larger that the button inside, which would render part of my own button unclickable. 我只需要确保里面的按钮不大即可,否则会使我自己的按钮的一部分无法点击。

What I would prefer, would be rendering that other element somewhere else and hiding it with display: none or position: absolute outside of my viewport and then triggering the click inside it. 我希望将其他元素渲染到其他position: absolute并使用display: noneposition: absolute将其隐藏在我的视口之外,然后触发其中的单击。 Due to modern CORS policies, as far as I know it's not possible to reach elements inside the <iframe> though - am I right? 根据现代CORS政策,据我所知,虽然无法到达<iframe>内部的元素-是吗?

Is there any more reliable way to achieve the same effect without so much trickery? 有没有更可靠的方法来达到相同的效果而又没有太多的诡计? I'm not that excited about opacity: .0001 , it make me anxious that in some browsers it will leave some visible trace of the other button. 我对不opacity: .0001并不感到兴奋opacity: .0001 ,这使我感到焦虑,因为在某些浏览器中它将留下其他按钮的可见痕迹。

出于安全原因,父元素不可能触发iFrame中的按钮(或任何其他元素)点击。

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

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