Jquery CSS 不适用于 Post 中的 div

标签 jquery css

我有一个使用 jquery 的 post 方法获取 div 的页面。然后我尝试将 css 应用于 div,但它不起作用。当我使用 jquery 在页面上已有的 div 上应用 css 时,它适用于它们。

J查询:

$('#next').click(function() {

    $.post('getquestions.php', {number: number}, function(result){

        $('#questionArea').html(result);
    });

$('.quest').css( "border", "13px solid red" );  //this doesn't work

});

从 getquestions.php 回显(这工作正常):

    echo "<div id='question" . $question_number . "' class='quest'>";

最佳答案

这是因为你的 div 类是 quest当您执行 jquery 的 css 时不可用功能。自 $.post是一个异步函数,div 仅通过 $('#questionArea').html(result); 添加到 DOM什么时候$.post的回调被执行,所以你应该在那里做 css 调用。

应该是这样的:

$('#next').click(function() {

    $.post('getquestions.php', {number: number}, function(result){

        $('#questionArea').html(result);
        // the div is just added
        $('.quest').css( "border", "13px solid red" );
    });
    // the div is not added to DOM yet since $.post has not finished fetching the result

});

关于Jquery CSS 不适用于 Post 中的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337706/

相关文章:

javascript - 如何使用javascript或jquery修改一个:before psedo-element attribute?

jquery - JQtree:将 url 与节点关联

css - 如何使用 CSS 将表单在 div 中垂直居中

javascript - 试图让我的照片幻灯片不断循环播放所有照片

javascript - 生成透明图像的 html2canvas 插件。

javascript - Flot 刻度转子显示刻度两次

javascript - 更改Google街景API的图像日期

html - 我的 img 去哪儿了?

javascript - Cufon 颜色被覆盖

javascript - 如何在点击(初始化)时设置一个 ng-click 按钮?