JavaScript代码在我的项目中不起作用

时间:2018-12-16 13:26:25

标签: javascript css asp.net

您好,我的JavaScript代码无法正常工作。 像这样的代码可以完美地工作:

function Choice() {
    var box = document.getElementById('searchh');

    if (!count) {
        var count = 0;
    }

    box.onclick = function () {
        count++;
        if (count == 1) {
           box.style.backgroundColor = "red";
       }
        else if (count == 2) {
            box.style.backgroundColor = "green";
        }
       else if (count == 3) {
            box.style.backgroundColor = "yellow";
       }
        else {
           count = 0;
           box.style.backgroundColor = "transparent";
       }
   }

}

但是像这样的代码:不起作用:

$(document).ready(function () {
              $(".divsec").on("click", function () {
                  $(this).css("background", "red");
              });
          });

这是我的html代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/PersonelMaster.master" AutoEventWireup="true" CodeFile="temsilci.aspx.cs" Inherits="temsilci" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
    <title>Hoşgeldin Temsilci</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
      <link href="css/fontawesome.min.css" rel="stylesheet" type="text/css" />
      <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
      <link href="css/Main.css" rel="stylesheet" type="text/css" />
      <script src="js/Main.js" type="text/javascript"></script>  
      <script language=javascript>
          $(document).ready(function () {
              $(".divsec").on("click", function () {
                  $(this).css("background", "red");
              });
          });
      </script>

</head>
<body>
<asp:Repeater ID="rp_Temsilci_isler" runat="server">
        <HeaderTemplate>
        <table id="temsilciheader" class="temsilcitable table-hover table-condensed" border="1" width="1000px">
            <tr class="headerturuncu">
                <td><div onclick="sortTable(0)"></div></td>
                <td><div class="markadiv">Marka<div class="arrow" onclick="sortTable(1)"></div></div></td>
                <td><div class="markadiv">İşin Adı<div class="arrow" onclick="sortTable(2)"></div></div></td>
                <td><div class="markadiv">Durum<div class="arrow" onclick="sortTable(3)"></div></div></td>
                <td><div class="markadiv">Veriliş Tarihi<div class="arrow" onclick="sortTable(4)"></div></div></td>
                <td><div class="markadiv">Teslim Tarihi<div class="arrow" onclick="sortTable(5)"></div></div></td>
                <td><div class="markadiv">Marka Temsilcisi<div class="arrow" onclick="sortTable(6)"></div></div></td>
                <td><div class="markadiv">Ajans Temsilcisi<div class="arrow" onclick="sortTable(7)"></div></div></td>
                <td><div class="markadiv">Sorumlu Kişiler<div class="arrow" onclick="sortTable(8)"></div></div></td>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>

            <tr>
                 <td> <div id="searchh"  class="divsec"></div> </td>
                 <td> <div class="isin_tanimi"><%#Eval("isin_tanimi") %></div></td>
                 <td> <div class="isin_tanimi"><%#Eval("isin_adi") %></div> </td>   
                 <td> <div class="isin_tanimi"><%#Eval("is_durum") %></div> </td>   
                 <td> <div class="isin_tanimi"><%#Eval("verildigi_tarih") %></div> </td>
                 <td> <div class="isin_tanimi"><%#Eval("teslim_tarihi") %></div> </td>
                 <td> <div class="isin_tanimi"><%#Eval("sorumlu_marka") %></div> </td>
                 <td> <div class="isin_tanimi"><%#Eval("sorumlu_ajans") %></div> </td>
                 <td> <div class="isin_tanimi"><%#Eval("revize") %></div> </td>
            </tr>                    
        </ItemTemplate>
        <FooterTemplate>
        </table>
        </FooterTemplate>
        </asp:Repeater>

这是我的CSS:

.divsec
{
    background:white;
    height:15px;
    width:15px;
    border:1px solid #ffcb31;
}

1 个答案:

答案 0 :(得分:0)

在声明代码之前添加<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>

$(document).ready(function () {
              $(".divsec").on("click", function () {
                  $(this).css("background", "red");
              });
          });