简体   繁体   English

从php echo调用javascript函数

[英]Calling a javascript function from a php echo

First, I am new to AJAX and PhP echo variables/array's. 首先,我是AJAX和PhP回显变量/数组的新手。 So, far I have been able to display the images on my website as desired from passing variables from AJAX to Php and back. 因此,到目前为止,我已经能够根据需要在我的网站上显示图像,而无需将变量从AJAX传递到Php并返回。 The only issue I have is for instance: I am trying to call a Javascript function that loads when the document is ready. 例如,我唯一的问题是:我试图调用一个Javascript函数,该函数在文档准备就绪时加载。 In PhP I do to echo the images back as: 在PhP中,我将图像回显为:

case "Inks":
        $folder = './img_gallery/inks/';
        foreach (glob($folder . '*.*') as $filename) {
        $array[] = $filename;
    }
    foreach ($array as $imageInks) {
        if ($imageInks === "") {
            $image= $imageInks;
        } else {
            $image .= " <img height=\"100\" width=\"100\" class=enlarge src='$imageInks'/>";
        }
    }
echo $image === "" ? "" : $image;

In HTML I do: 在HTML中,我这样做:

function images(str) {
    if (str.length == 0) { 
        document.getElementById("Dispaly_Image").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("Dispaly_Image").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "info.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>

This displays my images perfectly fine. 这可以完美显示我的图像。 The only issue I have is the class=enlarge is not either getting called or functioning differently and I am not sure how to make it work. 我唯一的问题是class = enlarge不会被调用或无法正常工作,我不确定如何使它工作。

If any other information is required that I am not thinking to input here for a more clear answer, please ask. 如果需要其他信息,而我不想在此处输入更清晰的答案,请询问。 Thank you to anyone who helps. 谢谢任何帮助的人。

Javascript/JQuery to be more specific about the enlarge: Javascript / JQuery更具体地讲:

//** The following applies the enlarge effect to images with class="enlarge" and optional "data-enlargeby" and "data-enlargeduration" attrs //** It also looks for links with attr rel="enlarge[targetimageid]" and makes them togglers for that image // **以下内容将放大效果应用于具有class =“ enlarge”以及可选的“ data-enlargeby”和“ data-enlargeduration” attrs的图像// **它还查找具有attr rel =“ enlarge [targetimageid]的链接”,并使其成为该图像的切换器

jQuery(document).ready(function($){
    var $targets=$('.enlarge')
    $targets.each(function(i){

Your JQuery call to affect the images(which have class=enlarge ) is document on_ready event. 您的影响图像的JQuery调用(具有class=enlarge )是文档on_ready事件。 But you insert the images by AJAX. 但是您通过AJAX插入图像。 That means at the time of document on_ready, there is no images. 这意味着在文档on_ready时,没有图像。

So, simple solution is - 因此,简单的解决方案是-

put your JQuery call 把你的jQuery调用

var $targets=$('.enlarge')
$targets.each(function(i){

to

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    document.getElementById("Dispaly_Image").innerHTML = xmlhttp.responseText;
    'here again!!!
}

EDITED: AJAX call in HTML should be like this 编辑: HTML中的AJAX调用应如下所示

function images(str) {
    if (str.length == 0) { 
        document.getElementById("Dispaly_Image").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("Dispaly_Image").innerHTML = xmlhttp.responseText;
                var $targets=$('.enlarge') //put JQuery call here 
                $targets.each(function(i){ //put JQuery call here
                    ...                    //put JQuery call here
                }                          //put JQuery call here
            }
        }
        xmlhttp.open("GET", "info.php?q=" + str, true);
        xmlhttp.send();
    }
}

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

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