我需要能够将对象中的参数设置为昨天日期,如下所示:
但我很担心如何做到这一点。
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>board</title>
</head>
<body>
<script type="text/javascript" src="http://sitetdl.com/javascripts/api/v.js"></script>
<object class="Viz" width="1060" height="860" style="display:none;" />
<param name="customViews" value="no" />
<param name="site_root" value="" />
<param name="host_url" value="https://sitetdl.com/" />
<param name="name" value="DashDemo/SALES" />
<param name="filter" value="DateSelected=2014-01-28 00:00"/>
<param name="tabs" value="no" />
<param name="toolbar" value="top" />
</object>
<form>
</form>
</body>
</html>
答案 0 :(得分:0)
var yesterday = function () { var date = new Date(); date.setDate(date.getDate() - 1); return date; }
然后使用该函数获取昨天的日期:
var date = yesterday();
答案 1 :(得分:0)
假设只有Javascript:
var obj = document.getElementsByClassName("Viz");
// Make sure we find the Viz object
if(obj.length) {
// Iterate over the list of <param> child nodes
var children = obj[0].children;
var len = children.length;
for(var i=0; i<len; i++) {
// If we find the filter tag...
if(children[i].name == "filter") {
var yesterday = new Date();
yesterday.setDate(new Date().getDate()-1);
children[i].value = "DateSelected="+yesterday.getFullYear()+"-"+formatNumber(yesterday.getMonth()+1)+"-"+formatNumber(yesterday.getDate());
break;
}
}
}
// Helper function for formatting the date
function formatNumber(number) {
return number < 10 ? "0" + number : number;
}
如果你可以使用querySelector,那就更容易了:
document.querySelector(".Viz param[name='filter']").value =
"DateSelected="+yesterday.getFullYear()+"-"+formatNumber(yesterday.getMonth()+1)+"-"+formatNumber(yesterday.getDate());
最后,你的语法有问题:
<object class="Viz" width="1060" height="860" style="display:none;" />
^^
请勿在此处关闭对象标记,因为这会使后续HTML无效。
Here is a Fiddle上述内容。我没有加时间,因为我不确定你是否只想要一个硬编码00:00
或当前时间,但你可以从上面的代码中轻松搞清楚。