簡體   English   中英

如何使用圖片上的OneClick刷新div?

[英]How do i refresh a divs with oneClick on a image?

我已經搜索過,但我無法理解,因為沒有人解釋。 所以這就是我想要做的:我想刷新div而不重新加載整個頁面。 像這樣。:

 <div id="topimage">
 <span id="happysmile"><img alt="happysmile" src="happysmile.png"></a></span>
 <span id="refreshbuttn"><img alt="refreshbuttn" src="refreshbuttn.png"></a></span>
                </div>

<div id="topDatum"><script type="text/javascript">
var mynewdate = new Date();
document.write(mynewdate.getMilliseconds());
</script></div>

所以我只想重新加載“ sec” div,而不是topimage div。 只需單擊refreshbuttn.png。

這可能嗎,你怎么做?

謝謝,對不起,我的英語不好。

如果用“ sec” div表示“ topDatum” div,則可以執行以下操作:

示例: http //jsfiddle.net/YCM9m/1/

<div id="topimage">
    <span id="happysmile"><img alt="happysmile" src="happysmile.png"></span>
    <span id="refreshbuttn"><img alt="refreshbuttn" src="refreshbuttn.png"></span>
</div>

<div id="topDatum"></div>

<script type="text/javascript">
    document.getElementById('refreshbuttn').onclick = function() {
        var mynewdate = new Date();
        document.getElementById('topDatum').innerHTML = mynewdate.getMilliseconds();
    }
</script>

這使用document.getElementById來獲取ID為refreshbuttn的元素。 它為它提供一個onclick處理程序,該處理程序創建一個日期對象,並獲取ID為topDatum的元素並將其innerHTML設置為mynewdate.getMilliseconds()的值。

如果您使用jQuery,你可以使用以下命令:

$("#refreshbuttn").click(function(){
    var mynewdate = new Date();
    $("#topDatum").html(mynewdate.getMilliseconds());
});

它將偵聽ID為 refreshbuttn的對象的點擊(在這種情況下,該對象為refreshbuttn span)
當它是一個新的日期。
然后,它將ID為 topDatum的對象的HTML設置為新的毫秒(在這種情況下,該對象為topDatum DIV)

在這里演示

這可能會幫助您入門: http : //jsbin.com/iwebo4/edit 這是JavaScript部分:

function updateMilliseconds() {
  if (document.getElementById('hello')) {
    document.getElementById('hello').innerHTML
      = new Date().getMilliseconds();
  }
}

updateMilliseconds();​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM