duancongduo4109 2018-04-15 09:11
浏览 49
已采纳

根据php中数组的大小更改html代码

Hi i have an array with some Urls and Html code for open this webs in others tabs. But the array dynamically changes the amount of Urls it contains. How can I change the Html code depending on the number of Urls that my array contains?

Code:

<?php
//$myarray can change dinamicaly the amount of urls contains
$myarray=array('www.google.com','www.piza.com','www.5.com');
?> 

Now in my HTML code I have the code for open a tab, but i need open the array numbers of urls. In this case I need window.open('') three times.

<p><a href='#'
onclick='window.open('');
>Click to open webs</a></p>
  • 写回答

2条回答 默认 最新

  • dqjjw04440 2018-04-15 09:27
    关注
    <?php
    //$myarray can change dinamicaly the amount of urls contains
    $myarray=array('www.google.com','www.piza.com','www.5.com');
    
    $windowsOpen = '';
    foreach ($myarray as $value) {
        $windowsOpen .= "window.open('$value', '_blank');";
    }
    ?> 
    
    //now in my HTML code I have the code for open a tab, but i need open the array numbers of urls. In this case I need 'window.open('')' three times.
    <p><a href='#' onclick="<?= $windowsOpen; ?>">Click to open webs</a></p>
    

    Few things you should know, the URLs need to start with 'http://' or 'https://' in order to be an external link, if not it be considered as an internal link. Moreover, most of the browsers will block the window.open if it is more than one as spam.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?