Javascript在chrome中工作,而不是firefox或ie

前端之家收集整理的这篇文章主要介绍了Javascript在chrome中工作,而不是firefox或ie前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 javascript / jquery的新手.编写了以下脚本,它适用于Chrome 46.0.2490.80 m,但不适用于Firefox 42.0或IE 11.
格式为%< value_name>%的值是我正在使用的内容管理系统的预定义值.

如果布尔值“%asset_Metadata_register_button%”设置为0,或者在Date变量“eventDate”发生后的一天,该脚本应该隐藏按钮.否则显示按钮.

该脚本还应该在eventDate发生后更改按钮的文本和href值.

HTML

<p id="show"><a id="change" class="button" href="firstURl">Register</a></p>

使用Javascript

$(document).ready(function() {
    // Boolean value from Show Registration Button Metadata field
    var number = "%asset_Metadata_register_button%";

    // variable for start date of event
    var eventDate = new Date('%asset_attribute_start_date%');

    // variable for date at present time
    var now = new Date();

    // variable for one day after present time
    var oneDay = new Date('%asset_attribute_start_date%');

    oneDay.setDate(oneDay.getDate() + 1);

    // Hide button if Show Registration Button Metadata field is set to no,or for one day after Webinar took place. Otherwise show Button.
    if (number == 1) {
        if (eventDate < now && now < oneDay) {
            document.getElementById("show").innerHTML = "(Webinar will be uploaded shortly)";
        } else {
            $('#show').show();
        }
    } else {
        $('#show').hide();
    }

    // changes button text and link after webinar commences
    if (eventDate > now) {
        document.getElementById("change").innerHTML = "View Webinar";
        document.getElementById("change").href = "secondURL";
    }
});

解决方法

正如Jaromanda X所提到的那样,某些浏览器会出错.

您的代码如下所示:

// variable for start date of event
var eventDate = new Date('%asset_attribute_start_date%');

// variable for date at present time
var now = new Date();

//variable for one day after present time
var oneDay = new Date('%asset_attribute_start_date%');

oneDay.setDate(oneDay.getDate() + 1);

您可以将其替换为:

var dateTime = '%asset_attribute_start_date%';
var date = dateTime.substring(0,dateTime.indexOf(" "));
var time = dateTime.substring(dateTime.indexOf(" ")+1);

date = date.substring(date.indexOf("-")+1) + "-" + date.substring(0,date.indexOf("-"));

var eventDate = new Date(date + ' ' + time);
var oneDay = new Date();
oneDay.setDate(eventDate.getDate() + 1);

var now = new Date();

它的作用是将日期格式从“2015-11-04”替换为“11 -04-2015”.

猜你在找的JavaScript相关文章