gpt4 book ai didi

javascript - 多个 标签根据选中的标签更改 HTML 代码

转载 作者:可可西里 更新时间:2023-11-01 00:12:57 24 4
gpt4 key购买 nike

我有以下代码..

<!-- LANGUAGE -->
<div class="languages_box">
<span class="red">Languages:</span>
<a href="#" class="selected"><img src="../images/au.gif" alt="" title="" border="0" height="12px" width="15px"/></a>
<a href="#"><img src="../images/gb.gif" alt="" title="" border="0" height="12px" width="15px" /></a>
<a href="#"><img src="../images/de.gif" alt="" title="" border="0" height="12px" width="15px" /></a>
</div>

<!-- CURRENCY -->
<div class="currency">
<span class="red">Currency: </span>
<a href="#" class="selected">AUD</a>
<a href="#">GBP</a>
<a href="#">EUR</a>
</div>

产生以下结果..

My expected output

我正在尝试更改代码,以便当用户单击另一个标志或值时,它会更新网页以更改其中一个被选中的内容。为此,我会想象每个 <a>标签将包含 <?php if(something) {echo (class=\"selected\"); } ?>但我不确定 something 是什么会是。

有没有办法在 PHP 中做到这一点?还是因为页面内容已经加载,所以只能在 Ajax/jQuery 中使用?

最佳答案

<?php
session_start();

echo $_SESSION['flag'];
?>
<!-- LANGUAGE -->
<div class="languages_box">
<span class="red">Languages:</span>
<a href="#" class="selected" id="1"><img src="../images/au.gif" alt="" title="" border="0" height="12px" width="15px"/></a>
<a href="#" id="2"><img src="../images/gb.gif" alt="" title="" border="0" height="12px" width="15px" /></a>
<a href="#" id="3"><img src="../images/de.gif" alt="" title="" border="0" height="12px" width="15px" /></a>
</div>

<!-- CURRENCY -->
<div class="currency">
<span class="red">Currency: </span>
<a href="#" <?php if ($_SESSION['flag'] == 1) { ?> class="selected" <?php } ?>>AUD</a>
<a href="#" <?php if ($_SESSION['flag'] == 2) { ?> class="selected" <?php } ?> id="gbp">GBP</a>
<a href="#" <?php if ($_SESSION['flag'] == 3) { ?> class="selected" <?php } ?>>EUR</a>
</div>
<style>
.selected{
color:green;
font-size:20px;
}
</style>
<script src="js/jquery.min.js" type="text/javascript"></script>

<script>
$(document).ready(function () {
$('.languages_box a').click(function ()
{
var flagid = ($(this).attr('id'));
var jsonData = {"flagid": flagid};
$.ajax({
type: 'POST',
async: false,
url: "testadmintest.php",
data: jsonData,
success: function (data) {
//alert(data);
window.location.reload();
// $('.currency a').removeClass('selected');
// if($.trim(data)==1){
// $('#gbp').addClass('selected');
// }

}
});
})
});
</script>

<---- php代码------>

<?php

session_start();
unset($_SESSION['flag']);
if (isset($_POST['flagid'])) {
$flagid = $_POST['flagid'];
if ($flagid == 1) {
$_SESSION['flag'] = 1;
} else if ($flagid == 2) {
$_SESSION['flag'] = 2;
} else {
$_SESSION['flag'] = 3;
}
}

//echo"1";
?>

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