在同一页上打印具有相同功能的数组中的不同内容
我想知道是否可以在类中拥有一个函数,您可以在同一页面上调用该函数,但它需要回显数组中的不同内容。
我的意思是:
如果这是我的数组:
| Day | Comment |
|-----|---------|
| 1 | hallo |
|-----|---------|
| 2 | hey |
|-----|---------|
| 3 | hello |
|_____|_________|
如果我单击第 1 天,则应显示第 1 天的数组中的内容,如果我单击第 2 天,则应显示第 2 天的内容。
是否可以创建一个函数来执行此操作,并且我可以在同一页面上使用相同的函数进行回显。 喜欢:
--------------------------------------------
| <div id="day1"> content day 1 </div> |
| <div id="day1"> content day 2 </div> |
| <div id="day1"> content day 3 </div> |
--------------------------------------------
I was wondering if it's possible to have a function in a class, that you call on the same page, but it needs to echo different stuff from the array.
What I mean:
If this is my array:
| Day | Comment |
|-----|---------|
| 1 | hallo |
|-----|---------|
| 2 | hey |
|-----|---------|
| 3 | hello |
|_____|_________|
And if I click on day 1, the content from the array where day = 1 should appear, if I click day 2, the content of day 2 should appear.
Is it possible to create a function does this and that I can echo on the same page with the same function.
Like:
--------------------------------------------
| <div id="day1"> content day 1 </div> |
| <div id="day1"> content day 2 </div> |
| <div id="day1"> content day 3 </div> |
--------------------------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您使用 JQuery 是可能的,但这可能看起来像一个中间解决方案,所以我会尽力解释尽可能多的内容。您可以参考下面的代码并将它们放在与您的 PHP 相同的页面中。
1)首先在标头加载 Jquery,并假设您的数组变量名为
myarray
。2)最好将以下脚本部分添加到页面顶部。
3)代码解释,Jquery通过定位任何元素的
id
或class
来工作并执行一些操作。当用户单击或执行操作时(在您的情况下,我假设单击 div),Jquery 将引用上面的脚本并查看它是否与任何 div 的 id 匹配。如果找到匹配,则会触发点击功能并执行任务。4)
$(this).html
是一个在 div 内容之间显示 html 或文本的函数,在本例中这将是数组的值。this
表示用户单击的元素,并根据id
名称而变化。尝试一下,您会对 Jquery 的功能印象深刻,很快您就会对它上瘾。 :) 干杯!
5) 一个更具可重用性和动态性的函数,当添加更多时,它会裁剪出 id 的名称以选择数组。
在 div html 代码中,请向所有 div 添加一个类,如下所示。
我还没有测试这个,但这应该可以工作,它将把 div 的 id 剥离为一个数字,然后减去 1,因为数组以 0 开头。然后将数组的值加载到单击它的 div 中。
Yes, it is possible if you use JQuery but this might looked like an intermediate solution so I will try my best to explain as much as possible. You can refer to the code below and placed them in the same page as your PHP.
1) Firstly Load Jquery at your header and lets assumed your array variable is named
myarray
.2) Add the below script section preferably on top of your page.
3) Explanation of the code, Jquery works by locating
id
orclass
of any element and performed some action. When a user clicks or perform an action (in your case, I would assumed by clicking the div) Jquery will refer to the script above and see if it matches any div's id. If a match is found, it will trigger the click function and perform a task.4)
$(this).html
is a function to display html or text between the div's content, in this case that would be your array's value.this
means the element that the user clicks and varies based onid
name.Try it out, you will be impressed with what Jquery can do and soon you will be addicted to it. :) Cheers!
5) A more reusable and dynamic function that will crop out
id's
name to select the array when more are added.In the div html code, please add a class to all the div like below.
I didn't test this yet but this should work, it will strip the div's id down to a number then subtract by 1 because array starts with 0. Then load the array's value into the div's that clicks it.