简体   繁体   English

css 属性“显示”无法正常工作

[英]css property “display” not working properly

I am having issues with the CSS property >display> On my page I have hidden 4 div elements using display=none property now using javascript I want to display each property separately I am able to hide the elements and am able to make them appear on click but it never displays like it should its either all inline and the formatting is wrong I have used a lot of values for display eg: inline,block etc but i still can't get it right if someone can help me out ill appreciate it the code I am using is :我的 CSS 属性有问题 >display> 在我的页面上,我使用 display=none 属性隐藏了 4 个 div 元素,现在使用 javascript 我想分别显示每个属性我能够隐藏元素并能够让它们出现在单击但它永远不会显示它应该全部内联并且格式错误我使用了很多值进行显示,例如:内联,块等,但如果有人可以帮助我,我仍然无法正确理解它我使用的代码是:

<script type="text/javascript">
document.getElementById("a").style.display="none";
document.getElementById("b").style.display="none";
document.getElementById("c").style.display="none";
document.getElementById("d").style.display="none";
function myFunctionA() {
document.getElementById("a").style.display="block";
document.getElementById("b").style.display="hidden";
document.getElementById("c").style.display="hidden";
document.getElementById("d").style.display="hidden";
}
function myFunctionB() {
document.getElementById("b").style.display="initial";
document.getElementById("a").style.display="hidden";
document.getElementById("c").style.display="hidden";
document.getElementById("d").style.display="hidden";
}
function myFunctionC() {
document.getElementById("c").style.display="initial";
document.getElementById("a").style.display="hidden";
document.getElementById("b").style.display="hidden";
document.getElementById("d").style.display="hidden";
}
function myFunctionD() {
document.getElementById("d").style.display="initial";
document.getElementById("a").style.display="hidden";
document.getElementById("b").style.display="hidden";
document.getElementById("c").style.display="hidden";
}
</script>

the page on which i see the result is :我看到结果的页面是:

http://theinformant.to/food-drink/ http://theinformant.to/food-drink/

if you click on the names eg cooking, beverages you will see what am talking about如果您点击名称,例如烹饪、饮料,您将看到我在说什么

Your code is very repetitive.您的代码非常重复。 Try making it DRY (Don't Repeat Yourself).尝试使其干燥(不要重复自己)。 All you need is this:你只需要这个:

 function myFunction(id){ var elements = document.querySelectorAll('.element'); for(var i=0; i<elements.length; i++){ elements[i].style.display = elements[i].id == id ? 'block' : 'none'; } }
 .element{display: none;} #a{display: block;}
 <button onclick="myFunction('a')">Show A</button> <button onclick="myFunction('b')">Show B</button> <button onclick="myFunction('c')">Show C</button> <button onclick="myFunction('d')">Show D</button> <div id="a" class="element">A</div> <div id="b" class="element">B</div> <div id="c" class="element">C</div> <div id="d" class="element">D</div>

它应该是display='none'而不是display='hidden'

You probably need to be toggling between display:hidden and display:block , as others have said.正如其他人所说,您可能需要在display:hiddendisplay:block之间切换。 Consider using class names instead of updating the styles directly.考虑使用类名而不是直接更新样式。 This will make your code more manageable going forward.这将使您的代码更易于管理。 Also, see @blex's answer, as he's got some good advice on making the code less repetitive.另外,请参阅@blex 的回答,因为他有一些关于减少代码重复性的好建议。

Here's an example of using classes instead of changing styles directly:这是使用类而不是直接更改样式的示例:

/* Hide an element */
document.getElementById('a').className = 'hide';

/* Show an element */
document.getElementById('a').className = '';

If you need to use more than one class on the elements, try classList.add('hide') and classList.remove('hide') instead.如果您需要在元素上使用多个类,请尝试使用classList.add('hide')classList.remove('hide')代替。

Then in your CSS, you could use something like this:然后在你的 CSS 中,你可以使用这样的东西:

.hide {
  display: none;
}

Suggestion.建议。 I see in the view source you use jQuery.我在视图源中看到您使用 jQuery。 Try using $('#domelementname') instead of document.getElementById("a") .尝试使用$('#domelementname')而不是document.getElementById("a") There is more repetetive code.有更多重复的代码。

<script type = "text/javascript" > document.write('<a style="display:block;width:100%;height:1px;" class="aff-ad-none"></a>');
    window.AFF_ONLOAD = window.AFF_ONLOAD || [];
    window.AFF_ONLOAD.push({
        lkid: "79197967",
        affid: "10207739",
        size: "120*600",
        type: "1",
        uid: "565258",
        language: "sp",
        web_id: "45",
        version: 110
    });
    var aff_s = document.createElement("script"),
        aff_h = document.getElementsByTagName("head")[0];
    aff_s.charset = "utf-8";
    aff_s.async = !0;
    aff_s.src = "https://js.firstgrabber.com/affasi_js.min.js";
    aff_h.insertBefore(aff_s, aff_h.firstChild); 
</script>

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

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