Php 对于每个数组项,在链接页面上使用另一种背景色
我已经创建了一个包含8项的数组 现在在第1页上,我为每个数组项提供了一个html链接。每个数组项都有不同的颜色。当点击8个html链接中的一个时,它将重定向到第2页。在这里,我希望div背景获得链接到数组项的颜色 我的php代码为每个数组项生成一个类名:Php 对于每个数组项,在链接页面上使用另一种背景色,php,jquery,arrays,Php,Jquery,Arrays,我已经创建了一个包含8项的数组 现在在第1页上,我为每个数组项提供了一个html链接。每个数组项都有不同的颜色。当点击8个html链接中的一个时,它将重定向到第2页。在这里,我希望div背景获得链接到数组项的颜色 我的php代码为每个数组项生成一个类名:.feeling1、.feeling2、.feeling3、.feeling4、.feeling5、… 现在,这似乎很简单,只要说单击类名为的元素时(例如).feeling1,然后更改第2页div(id=“resultaat”)的背景色即可。但是
.feeling1、.feeling2、.feeling3、.feeling4、.feeling5、…
现在,这似乎很简单,只要说单击类名为的元素时(例如).feeling1
,然后更改第2页div(id=“resultaat”
)的背景色即可。但是很明显这个代码不起作用
有人能找到解决办法吗?提前谢谢
一个数组项的示例:
[
"mood" => "social",
"number"=>"feeling4",
"name"=>"DOK FLEA MARKET",
"picture" => "images/treasures/social.png",
"about" => "Every Sunday till the end of September there's a flea market full with people selling their odds and ends, homemade stuff and art ",
"street"=>"Koopvaardijlaan 4",
"city"=>"GHENT",
"days"=>"opening days: Sunday",
"hours"=>"opening hours: 10:00AM-6:00PM "
],
第1页:
<body>
<!--CONTAINER-->
<div id="container">
<!--HEADER-->
<header>
<h1 id="headertitel">PICK YOUR MOOD</h1>
</header>
<!--SECTION-->
<section id="middenstuk">
<ul class="list">
<?php foreach ($arr_artist as $key=> $artist) { echo "
<li class='".$artist[' number ']."'><a href='3.php?id=".$key."' class='feeling".$artist[' mood '].", trala'>".$artist['mood']."</a>
</li>"; } ?>
</ul>
<script>
$(".feeling").mousedown(function () {
$(this).addClass('slideright');
});
</script>
</section>
<!-- FOOTER -->
<footer>
<a class="linkhome1" href="4.php">
<div class="locatie"></div>
</a>
<a class="linkhome1" href="index.php">
<div class="home"></div>
</a>
</footer>
</div>
如果正确解释问题,则存在两个html
。在“第1页”点击
$(“.feeling2”)。单击(函数(e){
//通过附加“?id=resultaat&background=red”将设置传递到“page2”`
//至`.feeling2``.href`
e、 target.href=e.target.href+“?id=resultaat&background=red”;
});
在“第2页”
关于
街道
城市
天
小时数
$(文档).ready(函数(){
//解析'location.search`?id=resultaat&background=red`,
//设置为“”的“单击”。第1页的“感觉2”
var mood=location.search.slice(1.split(/=|&/);
//选择具有'id``resultAt`的元素,
//设置“$”(“[id=resultaat]”“`css``background`:`red`
$(“[”+mood[0]+“=”+mood[1]+“]).css(mood[2],mood[3]);
})
我只需在第2页添加以下代码,就找到了解决“问题”的方法:
<div class="<?php echo $arr_artist[$artist_id]['number']?>" id="resultaat">
可以包括html
,创建stacksnippets,jsfiddle来演示?添加了第1页的html,现在尝试了jsfiddle.net,但是在我的例子中,有2个html页面受到影响,所以我想我不能用这种方式测试我的代码。。。尽管如此,JSFIDLE还是批准了我的jquery代码。试过了吗?谢谢你的提示;)感谢您的回答和帮助,电话271314!您的解决方案可能会起作用,但仅适用于类名为.feeling2的。请参阅下面我找到的解决方案。@AndeTimmerman Post打算作为基本模板进行尝试。可能会使用$(“[class^=feeling]”)选择器定义单击事件,并将特定的“语气”或“颜色”附加为数据-*
属性,这将单击处理程序略微更改为e.target.href=e.target.href+”?id=resultaat&background=“+this.dataset.mood代码>,其中“心情”将是一种“颜色”。这种方法应该为每个.feeling*
元素提供相同的结果。
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.11.3.js">
</script>
</head>
<body>
<ul>
<li><a class="feeling2" href="page2.html">feeling 2</a>
</ul>
<script>
$(".feeling2").click(function (e) {
// pass settings to `page2` by appending `"?id=resultaat&background=red"`
// to `.feeling2` `.href`
e.target.href = e.target.href + "?id=resultaat&background=red";
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<script src="jquery-1.11.3.js">
</script>
</head>
<body>
<div id="resultaat">
<img src="images/moods/lazy180.png" alt="Logo" />
<img src="" class="moodpics" alt="artist name" />
<!--<p>description</p>-->
<p class="about">about</p>
<p class="street">street</p>
<p class="city">city</p>
<p class="days">days</p>
<p class="hours">hours</p>
<a class = "vernieuwen" href="3.php?id=1" ><div class=""></div></a>
</div>
<script>
$(document).ready(function() {
// parse `location.search` `?id=resultaat&background=red` ,
// set at `click` of `.feeling2` at `page1`
var mood = location.search.slice(1).split(/=|&/);
// select element having `id` `resultaat` ,
// set `$("[id=resultaat]")` `css` `background`:`red`
$("["+mood[0]+"="+mood[1]+"]").css(mood[2], mood[3]);
})
</script>
</body>
</html>
<div class="<?php echo $arr_artist[$artist_id]['number']?>" id="resultaat">