简体   繁体   English

CSS 如何干扰 JavaScript?

[英]How can CSS interfere with JavaScript?

Original question: How can i embed JavaScript function targets?原始问题:如何嵌入 JavaScript 函数目标?

(It seems after some testing by site users that I was doing it correctly... For the NEW QUESTION skip to the bottom) (经过网站用户的一些测试,我似乎做得对了……对于新问题,请跳到底部)

I posted an almost identical question a couple days ago, where I had succeeded in listening and writing to the page with document.write and document.addEventListener but was unable to target id descriptors.几天前我发布了一个几乎相同的问题,我已经成功地使用document.writedocument.addEventListener监听和写入页面,但无法定位 id 描述符。 As it turned out there were some typos and I was misusing .innerHTML .结果发现有一些拼写错误,我滥用了.innerHTML I targeted backDrop and after some corrections I was able to replace the div 's inside it with the output of a squaring function.我的目标是 backDrop ,经过一些更正后,我能够用平方函数的输出替换里面的div

This time all I want to do is target an id embedded one level deeper.这次我要做的就是针对嵌入更深一层的 id。 It appears to me that all I should have to do is swap the element variable from backDrop to addItem , but that isn't working.在我看来,我应该做的就是将元素变量从backDrop交换为addItem ,但这不起作用。 heh How can I get this to work, and is every step going to present a new road block? heh 我怎样才能让它发挥作用,每一步都会出现一个新的障碍吗?

HTML / HTML /

<div class="backDrop" id="backDrop">
 <div class="lineBreak" id="addItem"></div> <!-- class styled 16 pixels thick -->
 <div class="lineBreak"></div>
 <div class="interface">yo</div>
</div>

JavaScript / JavaScript /

var element = document.getElementById("addItem");
element.addEventListener('click', promptFunction);

function promptFunction() {
element.innerHTML = square(window.prompt('inputvar'));
}

function square(x) {
 return x * x;
}

 var element = document.getElementById("addItem"); element.addEventListener('click', promptFunction); function promptFunction() { element.innerHTML = square(window.prompt('inputvar')); } function square(x) { return x * x; }
 body { background-color: #3A3C3D; /*alt color #CCA #3A3C3D*/ margin: 0; overflow: hidden; /*top stop the extended shadow element height from causing the page to scroll*/ } .backDrop { background-color: #FFF; /*alt colors #ACA null #CCA*/ height: 100vh; width: 720px; margin: auto; } .backDrop:before { /*for to get rid of backDrop shadow round-corners*/ box-shadow: 0 -20px 20px 0 black; content:''; height: 200vh; position: absolute; /*not sure why this is necissary, but it I know it is.*/ width: 720px; } .interface { border-left: 2px solid red; border-radius: 5px; box-shadow: 0 0 5px -2px rgba(0,0,0,.4); margin: auto; height: 270px; width: 480px; } .lineBreak { background-color: rgba(255,0,0,.3); border-bottom: 1px dashed black; height: 15px; }
 <div class="backDrop" id="backDrop"><!-- --><div class="lineBreak" id="addItem"></div><!-- --><div class="lineBreak"></div><!-- --><div class="interface">yo</div> </div>

New question: How can CSS interfere with JavaScript?新问题:CSS 如何干扰 JavaScript?

According to Barmar, it seems that specifically the combination of CSS I used to style my background div and it's shadow are somehow preventing the Javascript from targeting id 's embedded within it.根据 Barmar 的说法,似乎特别是我用来设置背景 div 样式的 CSS 和它的阴影的组合以某种方式阻止了 Javascript 将id嵌入其中。 What's stranger is that commenting either one of them out fixes the problem (leaving my styling incomplete or incorrect).奇怪的是,评论其中任何一个都可以解决问题(使我的样式不完整或不正确)。 How is this possible?这怎么可能?

The problem is position: absolute in the .backDrop CSS.问题是position: absolute.backDrop CSS 中。 Removing that allows the click to go to the #addItem DIV, instead of going to .backDrop .删除允许点击转到#addItem DIV,而不是转到.backDrop

 var element = document.getElementById("addItem"); element.addEventListener('click', promptFunction); function promptFunction() { element.innerHTML = square(window.prompt('inputvar')); } function square(x) { return x * x; }
 body { background-color: #3A3C3D; /*alt color #CCA #3A3C3D*/ margin: 0; overflow: hidden; /*top stop the extended shadow element height from causing the page to scroll*/ } .backDrop { background-color: #FFF; /*alt colors #ACA null #CCA*/ height: 100vh; width: 720px; margin: auto; } .backDrop:before { /*for to get rid of backDrop shadow round-corners*/ box-shadow: 0 -20px 20px 0 black; content:''; height: 200vh; /* position: absolute; /*not sure why this is necissary, but it I know it is.*/ width: 720px; } .interface { border-left: 2px solid red; border-radius: 5px; box-shadow: 0 0 5px -2px rgba(0,0,0,.4); margin: auto; height: 270px; width: 480px; } .lineBreak { background-color: rgba(255,0,0,.3); border-bottom: 1px dashed black; height: 15px; }
 <div class="backDrop" id="backDrop"><!-- --><div class="lineBreak" id="addItem">click here</div><!-- --><div class="lineBreak"></div><!-- --><div class="interface">yo</div> </div>

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

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