简体   繁体   English

如何在AJAX函数中访问href标签的内容

[英]How can I reach content of href tag in AJAX function

I am learning AJAX and I have one question. 我正在学习AJAX,我有一个问题。 In html file I have an image: 在html文件中,我有一张图片:

<a href="#" class="show_hide" title="<?php echo $image; ?>" onClick="javascript:ajax_post();return false" >
    <img src="foto.php?file=<?php echo $image; ?>" alt="building thumb"/>
</a>

Then I have AJAX function. 然后我有AJAX功能。 I tried it on basic input example and everything worked OK. 我在基本输入示例上进行了尝试,一切正常。 Now my question is how can I reach "title" in AJAX function. 现在我的问题是如何在AJAX函数中达到“标题”。

function ajax_post(){
    var hr = new XMLHttpRequest();
    var url = "my_parse_file.php";
    var title = ($(this).attr("title")); // This is not working...
    alert(title); // Title is unidentified.
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("status").innerHTML = return_data;
        }
    }
    hr.send(title);
    document.getElementById("status").innerHTML = "processing...";
}

Thank you for your help. 谢谢您的帮助。

var title = $(".show_hide").attr("title"); // 

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

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