gpt4 book ai didi

javascript - 如何将 PHP echo DIV 值传递到对话框

转载 作者:行者123 更新时间:2023-12-03 07:44:45 26 4
gpt4 key购买 nike

每篇文章都位于 DIV 标签中。想要使每个 DIV 可点击,一旦我点击 DIV,对话框应该显示适当的 DVI$add->tbl_article_content, $add->tbl_article_image 我的 JS 对话框仅出现在第 1 个 DIV 中。我应该如何为所有 DIV 执行此操作并传递相关数据。

PHP

<div class="row">
<?php
foreach ($data as $value) {
echo "<div class='col-lg-3'>";
echo "<p id='target'>" . $value->tbl_article_header . "</p>";
echo "</div>";
}
?>

jQuery

$( "#target" ).click(function() {
alert( "relevant DIV database values ??? " );
});

最佳答案

您应该将其设置为class,而不是target作为ID

<div class="row">
<?php
foreach ($data as $value) {
echo "<div class='col-lg-3'>";
echo "<p class='target'>" . $value->tbl_article_header . "</p>";
echo "</div>";
}
?>

$( ".target" ).click(function() {
alert( $(this).html() );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class='col-lg-3'>
<p class='target'>Value 1</p>
</div>

<div class='col-lg-3'>
<p class='target'>Value 2</p>
</div>

<div class='col-lg-3'>
<p class='target'>Value 3</p>
</div>

更新

<div class="row">
<?php
foreach ($data as $value) {
echo "<div class='col-lg-3'>";
echo "<p class='target' data-article=".$add->tbl_article_content.">" . $value->tbl_article_header . "</p>";
echo "</div>";
}
?>

$( ".target" ).click(function() {
alert( $(this).attr('data-article') );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class='col-lg-3'>
<p class='target' data-article="Articel 1">Head 1</p>
</div>

<div class='col-lg-3'>
<p class='target' data-article="Articel 2">Head 2</p>
</div>

<div class='col-lg-3'>
<p class='target' data-article="Articel 3">Head 3</p>
</div>

关于javascript - 如何将 PHP echo DIV 值传递到对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35257765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com