简体   繁体   English

执行AJAX请求后未应用CSS

[英]CSS not applied after executing AJAX request

I am struggling with an issue after an AJAX request. 在AJAX请求之后,我正在努力解决问题。 I am requesting data from the database. 我正在从数据库中请求数据。 That itself works perfectly and I process the data and add it into a table within the PHP file. 这本身很有效,我处理数据并将其添加到PHP文件中的表中。 That table consists of a caption followed by the data. 该表包含一个标题,后跟数据。 This table is then transferred to the main page where it is displayed. 然后将该表传送到显示它的主页面。 My problem is that I am trying to format the buttons that I create in the PHP to be formatted as well. 我的问题是我正在尝试格式化我在PHP中创建的按钮以进行格式化。 Here's the PHP with the data request 这是带有数据请求的PHP

    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "SELECT LoginName FROM table";

    $res = mysqli_query($conn, $sql);

    if (mysqli_num_rows($res) > 0) {

        echo "<center>";
        echo "<table border='1'>";
        echo "<tr>";
        echo "<td>User</td>";
        echo "<td colspan=2 align=center>Available Functions</td>";
        echo "</tr>";

        while($row = mysqli_fetch_assoc($res)) {
            echo "<tr>";
            echo "<td>" . $row['User'] . "</td>";
            echo "<td><button type='button' class='smallbutton'>Delete User</button></td>";
            echo "<td><button type='button' class='smallbutton'>Change Password</button></td>";
            echo "</tr>";
        }
        echo "</table>";
        echo "</center>";
    } else {
        echo "";
    }

    mysqli_close($conn);

As you can see I assigned a class to the buttons. 如您所见,我为按钮分配了一个类。 I thought it was easy to use that as a formatting tag for CSS. 我认为这很容易用作CSS的格式化标签。 My css file looks like the following: 我的css文件如下所示:

.smallbutton {
font-size:16px;
padding: 10px 10px;
border: none;
background-color: #008CBA;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 3px 10px 0 rgba(0,0,0,0.19);
color: white; }

Every time the code is executed, which is on the refresh of the page, the table is created but the buttons look like those ugly standard buttons and the CSS is not applied. 每次执行代码时(在刷新页面时),都会创建表,但按钮看起来像那些丑陋的标准按钮,并且不应用CSS。 I was able to include the style into the PHP file which then worked but that is really not how I want to structure my page. 我能够将样式包含在PHP文件中然后工作,但这不是我想要构建我的页面的方式。 The PHPs are really only the data provider and I want to use CSS in a central location for the layouts. PHP实际上只是数据提供者,我想在布局的中心位置使用CSS。

I already use the same CSS just with another class name for other buttons and they, no matter where they are located on the page, work perfectly. 我已经使用相同的CSS与另一个类名称用于其他按钮,无论它们位于页面的哪个位置,它们都能完美地工作。 Just these ones in that table I create, do not want to work. 只是在我创建的那个表中的这些,不想工作。

The table is embedded in a div which has the ID "DbInfo" and is filled via innerHTLM. 该表嵌入在ID为“DbInfo”的div中,并通过innerHTLM填充。

Just in case here's also my AJAX command: 以防这里也是我的AJAX命令:

$(document).ready(function(){
$.ajax({
    method: "POST",
    url: "UserData.php",
    success: function(data){
        $("#DbInfo").innerHTML = data;
    }
});  });

Can anybody help? 有人可以帮忙吗?

Thanks... 谢谢...

You can add id to your button. 您可以为按钮添加ID。 Eg:- 例如:-

echo "<td><button type='button' class='button'>Delete User</button></td>";
echo "<td><button type='button' class='button'>Change Password</button></td>";

And set the css class from client side after you set innerHTML in your AJAX. 在AJAX中设置innerHTML后,从客户端设置css类。 Eg:- 例如:-

$(document).ready(function(){

$.ajax({
    method: "POST",

    url: "UserData.php",

    success: function(data){

        $("#DbInfo").innerHTML = data;

        $(".button").addClass("smallbutton");

    }

});  
});

NOTE:- It is a bad practice to get all HTML code from server side via AJAX. 注意: - 通过AJAX从服务器端获取所有HTML代码是一种不好的做法。 Try to get only the data from your server via AJAX and construct your HTML table in client side. 尝试通过AJAX从服务器获取数据,并在客户端构建HTML表。

Eg;- php code 例如; - php代码

if (mysqli_connect_errno()) {
    return;
}

$sql = "SELECT LoginName FROM table";

$res = mysqli_query($conn, $sql);
$data = array();
if (mysqli_num_rows($res) > 0) {
    while($row = mysqli_fetch_assoc($res)) {
       array_push($data, $row['User']);
    }
}

mysqli_close($conn);
echo json_encode($data);

jquery code jquery代码

$(document).ready(function(){
$.ajax({
    method: "POST",
    url: "UserData.php",
    success: function(data){
        var userData = JSON.parse(data);
//contruct HTML table using jquery here
    }
});

just if someone is interested. 只要有人有兴趣 I restructured my CSS and have explicit references. 我重新构建了CSS并有明确的引用。 Instead of addressing only the class I added the button type as well. 我没有只处理类,而是添加了按钮类型。 The new css looks like this: 新的css看起来像这样:

button[type=button].abutton {
    font-size:16px;
    padding: 15px 15px;
    border: none;
    background-color: #008CBA;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 3px 10px 0 rgba(0,0,0,0.19);
    color: white;
}

button[type=button].smallbutton {
    font-size:16px;
    padding: 10px 10px;
    border: none;
    background-color: #008CBA;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 3px 10px 0 rgba(0,0,0,0.19);
    color: white;
}

That allows me to address the items properly. 这使我能够正确地解决这些问题。 Thanks to all your answers and Eric, who deleted his answer, but brought me on the right track. 感谢你的所有答案和埃里克,他删除了他的答案,但让我走上正轨。

regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM