简体   繁体   English

javascript从PHP Echo更改锚标记标题值

[英]javascript change anchor tag title value from PHP Echo

I have a class assigned to an anchor tag called status_button. 我有一个分配给锚标记的类,称为status_button。 On click of the image associated with the anchor tag it runs the attached function. 单击与锚标记关联的图像后,它将运行附加功能。 Two variables are passed to a php script and a 3 piece data response is echo'ed back separated by semicolon's. 将两个变量传递给php脚本,并用分号将3个数据响应回显。 I have set up alerts to ensure the correct data is coming back from php. 我设置了警报以确保从php返回正确的数据。

The thing I need help with is how to change the anchor tag title value using the echo'ed response. 我需要帮助的是如何使用echo响应更改锚标记标题值。 There are 5 examples out of probably 20 that I have tried. 在我尝试过的20个示例中,有5个示例。 None of them work, but I get no error's either. 它们都不起作用,但是我也没有错误。 Any help would be greatly appreciated. 任何帮助将不胜感激。

$(".status_button").on('click', function () {
    var element = $(this);
    var I = element.attr("id");
    var id = $("#id" + I).val();
    var sname = $(this).attr("title");
    $.post("openclose.php", {
        id: id,
        sname: sname
    },      
    function (data) {
        var response = (data).split(";", 3);
        alert(response[0]);
        alert(response[1]);
        alert(response[2]);

        $("#messageA" + I).innerhtml = (response[0]);
        $("#messageA" + I).hide();
        $("#messageA" + I).fadeIn(1500);
        $("#messageB" + I).html(response[1]);
        $("#messageB" + I).hide();
        $("#messageB" + I).fadeIn(1500);

        ***$(this).attr("title",(response[2]));
        ***$(I).attr("title", (response[2]));
        ***$("#id" + I).attr("title" , (response[2]));
        ***document.getElementById(I).title = (response[2]);
        ***document.getElementById("#id" +I).setAttribute("title",(response[2]));
    });
    return false;
});

This will work: 这将起作用:

$("#1").attr("title", (response[2]));

Your attempts: 您的尝试:

    ***$(this).attr("title",(response[2])); //this is no longer referring to the clicked element when inside the callback
    ***$(I).attr("title", (response[2])); //Missing # for ID
    ***$("#id" + I).attr("title" , (response[2])); //Not your element ID
    ***document.getElementById("#id" +I).setAttribute("title",(response[2])); //Not your ID

Well.... I had to change a few things around to get this to work the way I needed it to. 好吧....我不得不改变一些事情以使它按我需要的方式工作。 First off I removed the title from my anchor tag and placed it in my div tag. 首先,我从锚标签中删除了标题,并将其放在div标签中。 I then reduced my response from my php file from 3 down to 2 pieces of data. 然后,我将来自php文件的响应从3减少到2数据。

$(".status_button").on('click', function () {
    var element = $(this);
    var I = element.attr("id");
    var id = $("#id" + I).val();
    var xname = $("#messageA" + I).attr('title');
    $.post("openclose.php", {
        id: id,
        xname: xname
    },      
    function (data) {
    var response = (data).split(";", 2);
        $("#messageA" + I).attr('title', (response[0]));        
        $("#messageB" + I).html(response[1]);
        $("#messageB" + I).hide();
        $("#messageB" + I).fadeIn(1500);

    });
    return false;
});

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

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