简体   繁体   English

使用Javascript切换CSS值

[英]Toggle CSS values using Javascript

I'm trying to modify the behavior of some web parts in Sharepoint (thus forcing IE down my throat) for our users who use the Project server pages. 我正在尝试为使用Project Server页面的用户修改Sharepoint中某些Web部件的行为(从而迫使IE瘫痪)。 I'm not really the best JavaScript guy, and this is driving me nuts. 我并不是最适合JavaScript的人,这真让我发疯。

On one webpart to display the work from Project, there is a subrow 'Planned' shown below the actual data entry row that clutters the view. 在一个Web部件上,它显示了Project的工作,在实际数据输入行下方显示了一个子行“ Planned”,该行使视图混乱。 We want to turn the 'Planned' row off. 我们要关闭“计划的”行。

I can do it with a simple three liner like this: 我可以用一个简单的三班轮来做到这一点:

<style type="text/css">
   .XmlGridPlannedWork {display:none;}
</style>

But the users want to toggle the lines on and off. 但是用户希望打开和关闭线路。 So I thought I'd try reading then writting the current CSS value like so: 所以我想我会尝试阅读,然后像下面这样写当前的CSS值:

<script type="text/javascript">

function toggle_PlannedLine()
var ObjPlanned = Document.getElementById("tr").getElementsByTagName("XmlGridPlannedWork");

for(var i=0;i<ObjPlanned.length;i++)
{
    if (OjbPlanned[i].display != "none")
    {
        // toggle the 'Planned' line off
        ObjPlanned[i].style.display = "none";
    }
    else
    {
        // toggle the 'Planned' line on
        ObjPlanned[i].style.display = "inline";
    }
}

return;
}

</script>

<button onClick="toggle_PlannedLine();">Toggle Planned Line</button>

The actual segment I'm targeting looks like this: 我要定位的实际细分如下:

<tr class="XmlGridPlannedWork" RowID="694810f9-e922-4321-9236-e495dd5048d9B" ID="GridDataRow">

Of course, when you click the button, the rows don't disappear. 当然,单击按钮时,行不会消失。 At this point, I'm pretty sure I'm missing something obvious, but like I mentioned, I'm no JavaScript guru. 在这一点上,我很确定自己缺少明显的东西,但是就像我提到的那样,我不是JavaScript专家。

Easiest Solution 最简单的解决方案

Ok, so my answer below should help you out, but here is another way to approach it that is much simpler: 好的,所以我在下面的回答应该会对您有所帮助,但是这是另一种更简单的解决方法:

CSS 的CSS

<style type="text/css">
   .XmlGridPlannedWork {display:none;}
   body.showPlanned .XmlGridPlannedWork { display: block}
</style>

HTML/JavaScript HTML / JavaScript

<script type="text/javascript">
function toggle_PlannedLine() {
    if(document.body.className.match(/\bshowPlanned\b/) > -1)
        document.body.className = document.body.className.replace(/\bshowPlanned\b/,'');
    else
        document.body.className += " showPlanned";
}
</script>

<button onClick="toggle_PlannedLine();">Toggle Planned Line</button>

Original Answer 原始答案

You were really close in the concepts you wanted, but as the other answers point out a number of things were missing. 您确实很接近想要的概念,但是正如其他答案所指出的那样,还有很多东西遗漏了。 I rewrote your function to work cross browser, and please ask if you have any questions about it: 我重写了您的功能,使其可以跨浏览器工作,请询问您是否对此有任何疑问:

<script type="text/javascript">

function toggle_PlannedLine() {
  var objs = [];

  if( document.querySelector){
     objs = document.querySelectorAll('tr.XmlGridPlannedWork');
  } else if (document.getElementsByClassName) {
     objs = document.getElementsByClassName('XmlGridPlannedWork');
  } else {
     var temp = document.getElementsByTagName('tr');
     for(var j = 0; j < temp.length; j++){
       if(temp[j].className.match(/\bXmlGridPlannedWork\b/) > -1){
         objs.push(temp[j]);
       }
     }
  }

  for(var i=0;i<objs.length;i++)
  {
      if (objs[i].style.display != "none")
      {
          // toggle the 'Planned' line off
          objs[i].style.display = "none";
      }
      else
      {
          // toggle the 'Planned' line on
          objs[i].style.display = "inline";
      }
  }
}

</script>

<button onClick="toggle_PlannedLine();">Toggle Planned Line</button>

For those arguing that jQuery is not a valid answer, please take the following code as an example of why jQuery is so easy to use. 对于那些认为jQuery不是有效答案的人,请以以下代码为例,说明jQuery为何如此易于使用。 All of the previous code is summed up like this: 前面的所有代码都是这样总结的:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
  $(function(){
    $('button.toggle').click(function(){
      $("tr.XmlGridPlannedWork").toggle();
    })
  })
</script>

<button class="toggle">Toggle Planned Line</button>

You forgot the opening brace for your function. 您忘记了功能的开头括号。

You are using getElementByTagName incorrectly. 您使用的getElementByTagName错误。 This function gets elements that match based on tag name (a, img, etc.) not CSS class. 此函数获取基于标签名称(a,img等)而非CSS类匹配的元素。 You can use jquery to accomplish what you want, or you can enumerate through every element on the page until you find the one you want. 您可以使用jquery完成所需的操作,也可以枚举页面上的每个元素,直到找到所需的元素为止。 There are some open-source implementations of this available online. 可以在线获取一些开源实现。 Your best bet, though, would be to add an id to the tag you care about, and then use getElementById . 不过,最好的选择是将一个ID添加到您关心的标签中,然后使用getElementById

Finally, Document should be document , and JavaScript is case sensitive. 最后, Document应该是document ,和JavaScript 区分大小写的。

Hope this helps! 希望这可以帮助!

document.getElementsByTagName looks for elements based on the name of their HTML tag, not their class attribute. document.getElementsByTagName根据其HTML标签的名称而不是其类属性来查找元素。 Newer (not IE) browsers have support for document.getElementsByClassName(), and there are open source functions that do the same thing, falling back on the browser-native versions where available. 较新的(不是IE)浏览器支持document.getElementsByClassName(),并且有开放源代码的功能可以完成相同的工作,但可以使用本地版本的浏览器。 This function will return a NodeList containing all the elements that use that class, and you can access each element and hide it through that list. 此函数将返回一个包含使用该类的所有元素的NodeList,您可以访问每个元素并将其隐藏在该列表中。

First, document should be lowercase in your var ObjPlanned declaration. 首先,在var ObjPlanned声明中, document应小写。 Second, getElementById returns an element based on a unique ID and you're passing it the element, or tag, name. 其次, getElementById返回基于唯一ID的元素,然后将其传递给元素或标签名称。 getElementsByTagName returns an array of elements matching a certain tag but you're passing it a className. getElementsByTagName返回与特定标签匹配的元素数组,但是您要向其传递className。 There is no 'getElementsByClassName' built in to JavaScript, but you can easily use Google to find a solution. JavaScript没有内置“ getElementsByClassName”,但是您可以轻松地使用Google查找解决方案。

Use jQuery . 使用jQuery It provides a very useful $.css() method, which does what you're looking for in a very simple fashion. 它提供了一个非常有用的$ .css()方法,该方法可以非常简单地完成您要查找的内容。

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

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