1

在 Javscript 函数中,我为标签赋值。已分配值,但标签属性正在更改为默认值。

document.getElementById("Label1").innerText = dispTime;

dispTime是 javascript 函数中的某个值

<asp:Label ID="Label1" runat="server" ToolTip="Current Time" ForeColor="BlueViolet" BorderStyle="Ridge" BackColor="ButtonHighlight"></asp:Label>

这是我的标签的定义方式,但在页面加载后标签属性更改为默认值

我的页面加载

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:InIt();", true);

我的 javascript 部分

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title>Time</title>
<script type="text/javascript">
function InIt() {
    var dateTime = new Date();
    var hours = dateTime.getHours();
    var minutes = dateTime.getMinutes();
    var seconds = dateTime.getSeconds();
    var day;
    if (hours < 11) {
        day = "AM";
    }
    else {
        day = "PM";
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    var dispTime = document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds + " " + day;
    setTimeout("InIt()", 1000);
    var updatetime = document.getElementById("Label1");
    updatetime.innerText = dispTime;
}
InIt();
</script>

<form id="form1" runat="server">
<div id="time" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
    <asp:Label ID="Label1" runat="server" ToolTip="Current Time" style="background-color:ButtonHighlight; border-style:ridge;"></asp:Label>
</div>
</form>

4

4 回答 4

0

在 jquery 的 document.ready 上赋值。

如果您在页面的页面加载上写了一些内容,则从java-script该值设置的值将再次更改.cs

于 2013-02-21T09:33:13.267 回答
0

在 pageload 事件中,您可以使用此代码调用 javascript 函数 AssignvaluetoLabel() 然后在函数中添加脚本

 protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, this.GetType(),  "Assign", "AssignvaluetoLabel();", true);
}


<html>
<head runat="server">
<title>Untitled Page</title>
<script >
function AssignvaluetoLabel()
{
var dispTime="some value";
document.getElementById("Label1").innerText = dispTime;

}
</script>
</head>

 <body>
<form id="form1" runat="server">
<div>
    <asp:ScriptManager  ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
于 2013-02-21T09:38:32.053 回答
0

尝试在您的页面中添加 jquery 文件引用下面的代码。

<asp:Label ID="Label1" runat="server" ToolTip="Current Time" ForeColor="BlueViolet" BorderStyle="Ridge" BackColor="ButtonHighlight"></asp:Label>
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            TestFunction("testvalue");
        });

        function TestFunction(dispTime) {
            $("#<%= Label1.ClientID %>").text(dispTime);
        }
    </script>
于 2013-02-21T09:51:33.297 回答
0

这是您更新的代码...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server"  >
<title>Time</title>
<script type="text/javascript">

function AssignvaluetoLabel() {
    var dateTime = new Date();
    var hours = dateTime.getHours();
    var minutes = dateTime.getMinutes();
    var seconds = dateTime.getSeconds();
    var day;
    if (hours < 11) {
        day = "AM";
    }
    else {
        day = "PM";
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }



  document.getElementById("Label1").innerText = hours + ":" + minutes + ":" + seconds + " " + day;

}

</script>
</head>
<body>

   <form id="form1" runat="server">
<div id="time" runat="server">
    <asp:ScriptManager runat="server">
    </asp:ScriptManager>
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="Label1" runat="server" ToolTip="Current Time" style="background-color:ButtonHighlight; border-style:ridge;"></asp:Label>
</div>
</form>
</body>

`

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "Assign", "AssignvaluetoLabel();", true);
}
于 2013-02-25T04:20:24.453 回答