2017-03-25 23 views
0

ディレクトリのアドレスをコピーするボタンを作成したいとします。 しかし、次のコードを使用して、私はfinal_arrayの最初の項目の値を取得しています。ボタンが機能しない

<html> 
<head> 
    <title>Directory Listing</title> 
    <script> 
     function copyToClipboard(text) { 
      window.prompt("Copy to clipboard: Ctrl+C, Enter", text); 
     } 
    </script> 
</head> 
<body> 

    <?php 
    $root = 'MyPictures/'; 

    $iter = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($root,   RecursiveDirectoryIterator::SKIP_DOTS), 
     RecursiveIteratorIterator::SELF_FIRST, 
     RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" 
     ); 

    $paths = array($root); 
    foreach ($iter as $path => $dir) { 
     if ($dir->isDir()) { 
      $paths[] = $path; 
     } 
    } 
    $count = sizeof($paths); 

    $new_arr; 
    $final_arr; 

    for($i=2;$i<$count;$i++) { 
     $new_arr[$i] = str_replace("MyPictures","", $paths[$i]); 
     $final_arr[$i] = str_replace("\\","/", $new_arr[$i]); 

     echo '<br>'; 
     echo '<button id="demo" onclick="copyToClipboard(document.getElementById(' . "'demo'" . ').innerHTML)">' . $final_arr[$i] . '</button>';  
     echo '<br>'; 
    } 
    ?> 

</body> 
</html> 

いずれのボタンもクリックすると、コピーダイアログボックスの最初の要素が表示されます。

答えて

0

まず、IDを使用してタグを指定します。IDはドキュメント内で一意である必要があります。これはDOMの基本です。

ので、このライン:

echo '<button id="demo" onclick="copyToClipboard(document.getElementById(' . "'demo'" . ').innerHTML)">' . $final_arr[$i] . '</button>'; 

は、以下のようにOR単にclassを使用する代わりに、静的demo IDの一意のIDを持つべきである:

echo '<button class="demo" onclick="copyToClipboard(document.getElementById(' . "'demo'" . ').innerHTML)">' . $final_arr[$i] . '</button>'; 

第二部:

あなたは単純にあなたのHTML DOM要素を比較的にthisというオブジェクトをfollとして使って渡すことができますows:

echo '<button id="demo_'.$i.'" onclick="copyToClipboard(document.getElementById(this.id).innerHTML)">' . $final_arr[$i] . '</button>'; 
関連する問題