English 中文(简体)
JS: Hiding DIV based on another DIVs content
原标题:JS: Hiding DIV based on another DIVs content

我期待着根据另一个DIV的具体案文隐藏一些DIV。 我的贾瓦特(以下)是徒劳的。

<><><><><>>><>>>>>

<div id="LEGEND">abAB</div>

<div id="small-a"></div> 
<div id="small-b"></div> 
<div id="big-a"></div> 
<div id="big-b"></div> 

If the LEGEND DIV contains the text a, then I want it to show only DIV small-a.
If the LEGEND DIV contains the text bA, then I want it to show only DIV small-b and big-a.

www.un.org/Depts/DGACM/index_spanish.htm The Javascript:

<script>
window.onload = function ShowHide{

    if (document.getElementById( LEGEND ).indexOf("a") > 0){
        document.getElementById( small-a ).style.display =  block ;}
    if (document.getElementById( LEGEND ).indexOf("b") > 0){
        document.getElementById( small-b ).style.display =  block ;}
    if (document.getElementById( LEGEND ).indexOf("A") > 0){
        document.getElementById( big-a ).style.display =  block ;}
    if (document.getElementById( LEGEND ).indexOf("a") > 0){
        document.getElementById( big-b ).style.display =  block ;}    
</script>
最佳回答

你们正在经历两件事。

  • 职能声明也应当如此。

    function functionName(args) { }

  • 你们必须用风格掩盖iv。 显示=“无”

例:

<div id="LEGEND">abB</div>

<div id="small-a" style="display: none;">This is small-a</div> 
<div id="small-b" style="display: none;">This is small-b</div> 
<div id="big-a" style="display: none;">This is big-a</div> 
<div id="big-b" style="display: none;">This is big-b</div>

<script>
function showElement(id) {
    document.getElementById(id).style.display = "block";
}

window.onload = function ShowHide() {
    var legend = document.getElementById("LEGEND").innerHTML;

    if(legend.indexOf("a") != -1) showElement("small-a");
    if(legend.indexOf("b") != -1) showElement("small-b");
    if(legend.indexOf("A") != -1) showElement("big-a");
    if(legend.indexOf("B") != -1) showElement("big-b");
}
</script>
问题回答

问题是,你的代码将另一个<编码>div要素修改到“条码>div时的条码级要素,已成为一个组级要素。 你们需要把他们放在不首先使用特别安全局的身上,然后在 Java语中披露。

相反:

<div id="LEGEND">abAB</div>       

<div id="small-a" style="display: none;"></div>        
<div id="small-b" style="display: none;"></div>        
<div id="big-a" style="display: none;"></div>        
<div id="big-b" style="display: none;"></div>

<script type="text/javascript">
  window.onload = function() {
    if (document.getElementById( LEGEND ).indexOf( a ) > 0) {
      document.getElementById( small-a ).style.display =  block ;
      ...
      // etc.
    }
  }
</script>

第一,努力确保窗口。 载荷:

window.addEventListener( load , ShowHide, false);

function ShowHide()
{...

第二,你应研究该要素的内联网:

if (document.getElementById( LEGEND ).innerHTML.match("a") == "a"){...

第三,如果发言还应包含其他内容(用真实姓名取代ivName):

else {
    document.getElementById( divName ).style.display =  none }

希望帮助!

~md5sum~

EDIT:

另外,我也认为这并不是100%的保证,但我认为,这是为了:

<代码>window.onload = 功能显示Hide{

完全失败。 我认为,应该:

<代码>window.onload =功能()。

如果我这样的话,我将这样做。 你们迫切需要触及超文本部分,一切都用 j印。

页: 1

并且你不需要为每个标签制定<代码><div id=“小型-X”风格=“显示:无;”>。

<body>
<script>

window.onload=function(){

    x=document.getElementsByTagName("div");

    //first hide everything with small- or big-

    for(i in x)
        if(/small-|big-/.test(x[i].id))
            x[i].style.display="none";

    //then turn on each tags based on LEGEND

    x= document.getElementById("LEGEND").innerHTML;
    for(i=0;i<x.length;i++)
        document.getElementById((x[i]<= Z ? big- : small- )+x[i].toLowerCase()).style.display= block ;

}
</script>
<div id="LEGEND">aAB</div>

<div id="small-a">a</div> 
<div id="small-b">b</div> 
<div id="big-a">A</div> 
<div id="big-b">B</div>
</body>

您需要制定<代码>型号: Property to none





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签