Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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或PHP在单击时刷新随机图像脚本_Jquery_Html - Fatal编程技术网

jQuery或PHP在单击时刷新随机图像脚本

jQuery或PHP在单击时刷新随机图像脚本,jquery,html,Jquery,Html,我一直在寻找,没有解决办法。我有这个html: <div class="content_wrap"> <script src="jslib/random_image.js"></script> </div> 和一个链接: <li><a href="javascript:location.reload();" target="_self">Random image</a></li> 但

我一直在寻找,没有解决办法。我有这个html:

<div class="content_wrap"> 
  <script src="jslib/random_image.js"></script>
</div>

和一个链接:

<li><a href="javascript:location.reload();" target="_self">Random image</a></li>
  • 但是我不想重新加载整个页面(lame),而是想刷新div(不确定是否可能),或者至少刷新
    random\u image.js


    如何使用jQuery实现这一点?或者PHP,或者其他什么?

    假设您在div中有img

    <div class="content_wrap"> 
      <script src="jslib/random_image.js"></script>
      <img id="random_image" href="">
    </div>
    
    
    function updateNewImage(){
    //make ajax to server 
    // get new url store it in variable new_href
    // update random_image  $("$random_image").attr("href",new_href);
    }
    <li><a href="javascript:updateNewImage();" target="_self">Random image</a></li>
    
    
    函数updateNewImage(){
    //将ajax应用到服务器
    //获取新url将其存储在变量new_href中
    //更新random_image$(“$random_image”).attr(“href”,new_href);
    }
    

  • 您可以在onclick上调用一个函数,该函数将使用ajax获取新的图像url,然后您可以更新img href位置

    尚未测试代码,但假设没有重大错误,这应该可以。我假设您有jQuery(也可以使用简单的js,但也可以使用jq):

    
    函数重载映像(imgId){
    字符串randomImgSrc=“anewimgsrc.png”;
    $(“#content_wrap”).find(“#”+imgId).attr(“src”,randomImgSrc);
    }
    
  • 看看你对另一个答案的回答,我知道你想做的不仅仅是刷新图像,你想加载代码,这些代码可以做一些“事情”,比如设置图像,设置标题等等


    要做到这一点,您需要做一些其他的事情,比如向页面添加一个新的脚本元素,它可以做任何您想做的事情。查看以了解如何执行此操作。

    您首先需要一种方法来引用您的DIV,例如id:

    <div class="content_wrap" id="randomImage">
        <script src="jslib/random_image.js"></script>
    </div>
    

    这是div中的一个javascript,它调用一个随机图像和一些标题文本。@brianjohnson,它是做什么的?
    <div class="content_wrap" id="randomImage">
        <script src="jslib/random_image.js"></script>
    </div>
    
    $('#randomImage').load('jslib/random_image.js?dummy='+Math.random(),
        function(res, status, xhr){
            if ('success' != status)
            {
                // Do something with the image, maybe load a default one
                return;
            }
    });