English 中文(简体)
如何根据其他页面中的内容更改图像链接
原标题:How to change an image link based on content in another page

我想在我的页面(第1页)上放一个图像,作为另一个页面(第2页)的链接,但我希望显示的图像是基于第2页内容的两个可能图像之一。

本例中的Page2是一个报告当前状态的wiki页面。如果没有问题需要报告,则第1页上的图像将为绿色。如果列出了问题,则图像将为红色。

我的计划是使用html注释标记嵌入<--法定--><--STATUSGREEN->在我的wiki页面上。然后我想我可以使用javascript、Jquery或类似的东西来检查我的评论是否存在,并相应地更改第1页上的图像。我只是不知道该怎么做。

我也对其他想法持开放态度。无论我想出什么解决方案,wiki用户都必须简单地进行更改,而不需要用户更改page1(它不是wiki的一部分)。

更新:

我最终做了什么:

$(document).ready(function() {
    statusCheck();
    setInterval(statusCheck,300000);
}

function statusCheck() {
    $.ajax({
        type: "GET",
        url: "path/to/wiki/MainPage.ashx",
        dataType: "html",
        cache: false,
        success: function(html) {
            $( #statusImage ).attr( src , function() {
                if ($(html).find("#statusGreen").length)
                {
                    return  /images/green.gif ;
                }
                else
                {
                    return  /images/red.gif ;
                }
            });         
        },
        error: function(request,status, error) {
            $( #statusImage ).attr( src ,  /images/red.gif );
        }       
    });
}

然后在wiki页面上,我只是将有问题的div上的id属性更改为statusGreen或statusRed,如下所示:

<div id="statusGreen">
<font size="5" color="green">No issues to report.</font>
</div>

并且我要更改的图像得到一个状态图像id:

<img id="statusImage" width="95" height="95" border="0" />
最佳回答

这是非常浪费的,因为你让所有维基页面的内容只检查一个单词的存在,无论如何,以下是如何做到的:

<div id="hidden_container" style="display: none;"></div>
<img src="" id="wikistatus"/>

function set_status() {
  var status_image =  images/green.gif ;
  $( #hidden_container ).empty();
  $( #hidden_container ).load( path/to/wiki.html #status , function() {
    var status = $( #hidden_container ).text();
    var is_green = status.indexOf( <!--STATUSGREEN--> );
    if(is_green === false) status_image =  images/red.gif ;
    $( #wikistatus ).attr( src , status_image);
    setTimeout( set_status(); , 5000); // execute again in 5 seconds
  });
}

$(document).ready(function() {
  set_status();
});

在wiki.html中,请确保将状态代码包装在容器中,并使用status作为id:

<div id="status"><!--STATUSGREEN--></div>
问题回答

为了更容易一点,你可以添加一个参数(如果可能的话,取决于你对维基的控制程度)

<;a href=“page2”><;img src=“page2?image”alt=“”/></a>

如果请求中包含参数“image”,则让wiki页面直接返回正确的图像





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.