简体   繁体   English

回声中的javascript(document.getelementbyid)无法正常工作(可能是引号使用错误)

[英]javascript (document.getelementbyid) in an echo not working (probably wrong usage of quotes)

i'm running an while (php) which builds rows while onclick they open modals. 我正在运行一会儿(php),它会在onclick时打开行,从而建立行。 but i'm not able to get te javascript properly in the echo. 但我无法在回显中正确获取javascript。

i've read a lot on the internet but none of them are using that much quotes as me. 我在互联网上读了很多书,但没有一个人像我一样使用过多的引号。

bit more detail then before. 比以前多一些细节。 there's a database which is called "nuujts" and contains an 'id', 'titel', 'subtitel', 'inhoud'(content), 'aafbeelding'(html img src=on server) 有一个名为“ nuujts”的数据库,其中包含一个“ id”,“ titel”,“ subtitel”,“ inhoud”(内容),“ aafbeelding”(服务器上的html img src =)

underneight is a example of a hardcoded version which now needs to be extracted from the mysql server underneight是一个硬编码版本的示例,现在需要从mysql服务器中提取该版本

document.getElementByID should be $row['id'] document.getElementByID应该为$ row ['id']

<li onclick="document.getElementById('2').style.display='block'" class="w3-border">
   <a href="#">
      <div class="w3-row">
         <img src="images/tumbnail.png" class="w3-image w3-third">
         <div class="w3-twothird"><h6>Leever Bontje Middig</h6>Bòntje middig geörganiseerd door V.V. De Tuinhagedisse</br></br>Laes verder -></div>
      </div>
   </a>
</li>
<div id="2" class="w3-modal">
   <div class="w3-modal-content w3-text-black w3-animate-zoom">
      <header class="w3-container w3-text-white" style="background-color: #005415;">
         <span onclick="document.getElementById('2').style.display='none'" class="w3-button w3-xlarge w3-hover-red w3-display-topright" title="Sjloete">&times;</span>
         <h2>Leever Bòntje Middig</h2>
      </header>
      <p>Bòntje middig geörganiseerd door vv de Tuinhagedisse</p>
      Bòntje middig geörganiseerd door vv de Tuinhagedisse veur miense oet Leeve én omsjtreke!</br>
      Mit o.a. optraejes van Toeter Thijs en Pyure.</br>
      De middig begint om 14.00 oer (zaal aope vanaaf 13.30 oer).</br>
      <p>Ein kaertje kost €5,00, daoveur krieg geer entree & koffie mit vlaai.</p>

      <p>Sjpisjaal veur de miense die van boete Leeve komme, is dur GRATIS verveur geregeld. Opsjtapplaatse: de Donderie en 't Paradies.</p>

      Kaertjes kinne besjteld waere via:</br>
      julia.orval@gmail.com óf jmhn@xs4all.nl</br>
      Gaef naam, adres, tillefoonnummer en 't aantal luuj door véúr 27 fibberwarie. VOL=VOL</br>

      Wilt geer meer informatie hubbe? Gebroek dan baovesjtaonde mailadressen of bel nao 06-16353489.
   </div>
</div>
<?php
   $sql = "SELECT * FROM nuujts";
   $result = $conn->query($sql);

   if ($result->num_rows > 0 ) {
      while ($row = $result->fetch_assoc()) {
         echo "<li onclick='document.getElementById(".$row['id'].").style.display'block'' class='w3-border'>";
            echo "<a href='#'>";
               echo "<div class='w3-row'>";
                  echo $row['aafbeelding'];
                  echo "<div class='w3-twothird'><h6>".$row["titel"]."</h6>".$row["subtitel"]."</br></br>Laes verder -></div>";
               echo "</div>";
            echo "</a>";
         echo "</li>";
         echo "<div id='".$row["id"]."' class='w3-modal'>";
            echo "<div class='w3-modal-content w3-text-black w3-animate-zoom'>";
               echo "<header class='w3-container w3-text-white' style='background-color: #005415'>";
                  echo "<span onclick='document.getElementById(".$row['id'].").style.display='none'' class='w3-button w3-xlarge w3-hover-red w3-display-topright' title='Sjloete'>&times;</span>";
                  echo "<h2>".$row["titel"]."</h2>";
               echo "</header>";
               echo "<p>".$row["subtitel"]."</p>";
               echo $row["inhoud"];
            echo "</div>";
         echo "</div>";
      }
   }
?>

nothing happens but the modal should open. 什么都没有发生,但应该打开模态。 i believe the issues lays in the quotes but i can't wrap my head around the right quote usage 我相信问题在于报价,但我无法正确使用报价

You can always simplify your PHP and not embed javascript but instead rely on javascript outside of PHP. 您总是可以简化您的PHP,而不是嵌入javascript,而是依靠PHP之外的javascript。

First, instead of onclick, output the id into a data attribute of id and add an additional class. 首先,取代的onclick,输出ID到的数据属性id和添加一个附加的类。 Then use javascript to add click event handlers. 然后使用javascript添加点击事件处理程序。

echo "<li data-id='{$row['id']}' class='show-modal w3-border'>";

echo "<span data-id='{$row['id']}' class='hide-modal w3-button w3-xlarge w3-hover-red w3-display-topright' title='Sjloete'>&times;</span>";


var objs = document.getElementsByClassName('show-modal');

for(var i = 0; i < objs.length; i++) {
  (function(index) {
    objs[index].addEventListener("click", function() {
       document.getElementById(this.dataset.id).style.display = 'block';
     })
  })(i);
}

var objs = document.getElementsByClassName('hide-modal');

for(var i = 0; i < objs.length; i++) {
  (function(index) {
    objs[index].addEventListener("click", function() {
       document.getElementById(this.dataset.id).style.display = 'none';
     })
  })(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM