隐藏没有div或类的JS元素

时间:2013-08-20 15:20:14

标签: javascript dynamics-crm-2011

我试图隐藏SSRS报告的工具栏。

我需要使用JS的具体原因(该报告将包含在CRM 2011仪表板中,我想从报告中删除工具栏。由于报告参数不起作用,我导入了报告控制解决方案我正在编辑使用JS的查看器。查看器是一个Html页面,它将报表嵌入为IFrame。 生成的Html代码是:

<table id="reportViewer_fixedTable" cellspacing="0" cellpadding="0" style="table-layout:fixed;width:100%;height:100%;">
    <tbody>
        <tr style="background-color:#C4DDFF;"> … </tr>
        <tr id="ParametersRowreportViewer" style="display:none;"> … </tr>
        <tr style="height:6px;font-size:2pt;display:none;"> … </tr>
        <tr>

工具栏在第4个tr中,直接选择它并试图隐藏它不起作用。

navCorrectorDiv = report.contentWindow.document.getElementById('reportViewer_Toolbar');
if (navCorrectorDiv != null) {
    navCorrectorDiv.style.display = "none";
}

我应该选择表reportViewer_fixedTable,我可以做,然后选择tbody元素,然后选择第四个tr。 有办法吗?可能没有jQuery。

3 个答案:

答案 0 :(得分:3)

案例:没有iframe

选择元素

作为jQuery选择器:

var selected;
selected = jQuery('#reportViewer_fixedTable');
… 
selected = jQuery('#reportViewer_fixedTable tbody');
…
selected = jQuery('#reportViewer_fixedTable tr:nth-child(4)');

隐藏所选内容:

selected.css('display', 'none');

或没有jQuery的现代浏览器:

var selected;
selected = document.querySelector('#reportViewer_fixedTable');
…
selected = document.querySelector('#reportViewer_fixedTable tbody');
…
selected = document.querySelector('#reportViewer_fixedTable tr:nth-child(4)');

并隐藏:

selected.style.display = 'none';

案例:Iframe中的内容

iframe可能会有问题,因为它可能是沙盒或内容可能来自不同的域。这可能导致XSS违规,在您的情况下,可能是不可修复的。

无论如何,我们走了:

//Select the first iframe (which might not be the right one in your case);
var elem = document.querySelector('iframe'); 

//And put it's body in a variable. We use the querySelector from the body 
//of the iframe.
var ibody = elem.contentWindow.document.body;

var table = ibody.querySelector('#reportViewer_fixedTable');
var tbody = ibody.querySelector('#reportViewer_fixedTable tbody');
var fourthtr = ibody.querySelector('#reportViewer_fixedTable tr:nth-child(4)');

table.style.display = 'none';
tbody.style.display = 'none';
fourthtr.style.display = 'none';

答案 1 :(得分:0)

我想你可以通过尝试找到nth Chid

来做到这一点

考虑这种方法:

HTML:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <table id="reportViewer_fixedTable" cellspacing="0" cellpadding="0" style="table-layout:fixed;width:100%;height:100%;">
    <tbody>
        <tr style="background-color:#C4DDFF;"> <td>1</td> </tr>
        <tr id="ParametersRowreportViewer" style="display:none;"><td>2</td> </tr>
        <tr style="height:6px;font-size:2pt;display:none;"> <td>3</td> </tr>
        <tr><td>FourthTR</td></tr>
    </tbody>
</table>
</body>
</html>

<强> JS:

$(function(){
console.log( $('#reportViewer_fixedTable tbody tr:nth-child(4) td:nth-child(1)').text());


  $('#reportViewer_fixedTable tbody tr:nth-child(4) td:nth-child(1)').addClass('FourthTR');
  $('.FourthTR').hide();
});

所以,我们要做的是,我们抓住桌子的第4个tr然后我们抓住了第4个tr的第1个孩子。完成后,我们会在运行中添加一个类FourthTR,然后使用jQuery.hide()隐藏该类。瞧,你已经完成了。

请参阅此处的工作示例:http://jsbin.com/ACam/1/edit。一如既往,请记住run with js

答案 2 :(得分:0)

我认为您不需要为此使用JavaScript

如果您可以访问ReportView控制器和ReportViewer.aspx.cs的服务器端代码,则可以设置属性

reportViewer.ShowToolBar = false

在该代码中。

或者,如果您有权访问并可以修改查看器页面标记(ReportViewer.aspx),则可以声明性地设置它:通过向ReportViewer控件声明添加ShowToolBar="false"

<rsweb:ReportViewer ID="reportViewer" runat="server" ... ShowToolBar="false">
</rsweb:ReportViewer>

如果这不是一个选项,您可以通过添加rc:Toolbar=false参数修改您传递给IFrame托管ReportViewer的网址

http://localhost/ReportServer/Pages/ReportViewer.aspx?%2fMyReport%2fBEA&rs:Command=Render&rc:Toolbar=false