我已经使用了以下代码,其中我使用了外部.js文件以及内联脚本函数。内联工作正常并且标签已更改为绿色,但是当我将相同功能放入.js文件时,它将显示错误的输出

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">


<title></title>

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="JScript1.js" type="text/javascript"></script>

<%--<script>
    function onLeave1(_input, _labelInput) {

        var char = /^[A-z]+$/;
        var labelValue = _labelInput;

        var check = _input.value;

        if (_input.value.match(char)) {



            $('.' + _labelInput).css("color", "green");
            //  var someVariable = document.getElementById("Label8").innerHTML;
            // alert(someVariable);


        }
        else {
            $('#Label8').css("color", "red");
        }
    }
</script>--%>
  <style type="text/css">
     .style1
     {
        height: 26px;
      }
     .style2
    {
        height: 26px;
        width: 131px;
    }
    .style3
    {
        width: 131px;
    }
   </style>


  </head>
  <body>
<form id="form1" runat="server">
<div>


 <table>

    <tr>

        <td class="style1">

            <asp:Label runat="server" ID="lbl1" Text="First Name:-"></asp:Label>

        </td>

        <td class="style2">

            <asp:TextBox ID="txtID2" runat="server" onblur="onLeave1(this,'Label8')"></asp:TextBox>

            <asp:Label class="Label8" runat="server"  Text="|" ForeColor="Red"></asp:Label>
        </td>



    </tr>



</table>
  </div>
</form>
 </body>
 </html>

最佳答案

尝试更改:

$('#Label8').css("color", "red");


至:

$('.Label8').css("color", "red");


因为您已将class="Label8"分配给标签

关于javascript - 如何将标签ID传递到.js文件并更改标签颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21955296/

10-10 00:21
查看更多