簡體   English   中英

有沒有一種更動態的方法來使用php和javascript調用多個標簽

[英]is there a more dynamic way to call multiple a tags using php and javascript

大家好,我試圖找到一種更動態的方法來一次在同一選項卡中打開多個彈出窗口。 這是我的下面的代碼。 可以,但是如果您有10個標簽怎么辦。 任何建議表示贊賞。

   <script>

function openwindow(){
    window.open("https://www.facebook.com/sharer/sharer.php?u=<?=urlencode($article['link'])?>&p[title]=<?=urlencode($article['a_headline'])?>&display=popup","width=100,hieght=100");
}

function openwindow1(){
    window.open("https://twitter.com/intent/tweet?text=<?=urlencode($article['a_headline'])?>&url=<?=urlencode($article['shorten'])?>&related=&via=proofEurope<?=urlencode($ks)?>","width=100,hieght=100");
}
</script>

<li><a href="javascript: openwindow()" rel="nofollow" target="_blank"><img src="<?=ASSETS?>/www/img/news/fb.png" /></a></li>

<li><a href="javascript: openwindow1()" rel="nofollow" target="_blank"><img src="<?=ASSETS?>/www/img/news/twt.png" /></a></li>

首先,您不應該在href中使用javascript。 它可以工作,但這不是“正確”的方法。 將href設置為#並將javascript置於onclick上:

<a href='#' onclick='openwindow();'>my link</a>

您也可以在onclick中調用window.open ...

<a href='#' onclick="window.open('http://example.com');">my link</a>

注意,我必須對包含單引號的字符串加雙引號,以避免轉義引號。

您具有URL和圖像的邏輯組合。 將它們按陣列匹配。 我將圖像作為鍵,將URL作為值,如下所示:

$links = array(
    "fb.png" => "https://www.facebook.com/sharer/sharer.php?u=".urlencode($article['link'])."&p[title]=".urlencode($article['a_headline'])."&display=popup",
    "twt.png" => "https://twitter.com/intent/tweet?text=".urlencode($article['a_headline'])."&url=".urlencode($article['shorten'])."&related=&via=NewsweekEurope".urlencode($ks),
);

現在,您可以遍歷數組:

foreach($links as $img=>$url)
    print "<a href='#' onclick=\"window.open('$url')\"><img src='$img'></a>";

暫無
暫無

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

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