Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery/AJAX–;凝视与非主演_Jquery_Html_Ajax - Fatal编程技术网

jQuery/AJAX–;凝视与非主演

jQuery/AJAX–;凝视与非主演,jquery,html,ajax,Jquery,Html,Ajax,我希望能够启动/取消启动任何给定的对话(例如Gmail)。当我单击“空”星标记重要内容时,我需要提交一些ajax,然后切换到星图像。反之亦然,当我点击一个带星号的对话时,我需要ajax提交并在成功后将“空”的星号切换回来 一些HTML(简而言之): 一些ajax: $(".__conversation_star").click(function() { jQuery.ajax({ type: 'POST', url: "./process.convers

我希望能够启动/取消启动任何给定的对话(例如Gmail)。当我单击“空”星标记重要内容时,我需要提交一些ajax,然后切换到星图像。反之亦然,当我点击一个带星号的对话时,我需要ajax提交并在成功后将“空”的星号切换回来

一些HTML(简而言之):

一些ajax:

 $(".__conversation_star").click(function() {
    jQuery.ajax({
        type: 'POST',
        url: "./process.conversation.php,
        data: {method: 'star'},
        cache: true,
        success: function() {
            // Toggle to un-starred .__star_n
        }
    });
 });

 $(".__conversation_star").click(function() {
    jQuery.ajax({
        type: 'POST',
        url: "./process.conversation.php",
        data: {method: 'star'},
        cache: true,
        success: function() {
            // Toggle to starred .__star_n
        }
    });
 });
是否有一种方法可以在ajax成功后执行切换?和/或有哪些其他方法可以更好地实现这一点


谢谢大家!

与其切换显示的图像,不如切换CSS类。因此,如果您的HTML设置如下:

<div class="conversation">
    <span class="star"></span>
    <span>blah blah blah blah</span>
</div>
现在,您只需在每个

下面是一些JavaScript示例

$(".star").click(function() {
    var $thisStar = $(this);
    
    $.ajax({
        type: 'POST',
        url: "./process.conversation.php",
        data: {method: 'star'},
        cache: true,
        success: function() {
            $thisStar.toggleClass("state-selected");
        }
        
    });
    
});


演示:我喜欢这是多么干净,这绝对是一个可行的方法。你对如何处理图像有什么想法?我看不出为什么我不能切换这个类,在这样做的过程中,“切换”了我最初计划使用的图形的背景图像,对吗?我只是完全按照你的建议实现了,除了切换了两个类中的背景图像,而不是背景色。谢谢你的回答这很有帮助!伟大的是的,我想你会使用BG图像而不是颜色。我很高兴这对你有用!
<div class="conversation">
    <span class="star"></span>
    <span>blah blah blah blah</span>
</div>
.conversation{
    padding:3px 5px;
    border: 1px solid #CCC;
    margin-top:3px;
}

.star{
    background-color: hsl(200, 90%, 90%);
    border:1px solid #333;
    display:inline-block;
    height:20px;
    width:20px;
}

.star.state-selected{
    background-color: hsl(60, 70%, 60%);
}
$(".star").click(function() {
    var $thisStar = $(this);
    
    $.ajax({
        type: 'POST',
        url: "./process.conversation.php",
        data: {method: 'star'},
        cache: true,
        success: function() {
            $thisStar.toggleClass("state-selected");
        }
        
    });
    
});