点击链接/按钮时更改div的内容

编程入门 行业动态 更新时间:2024-10-27 05:30:48
本文介绍了点击链接/按钮时更改div的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

基本上,我有一个网页,其中包含div id =content和div id =sidebar,

我想要做的是更改在链接/按钮在侧边栏中单击时,内容div中的内容没有每篇文章的serperate页面。

原因是;

我为多个乐队做了一个BIO页面,每个乐队都会有多个按钮,然后当用户点击按钮/链接时,内容会发生变化。

任何想法,我一直在寻找.click使用jQuery或可能是PHP,但不完全确定如何实现它。

解决方案

您可以将每篇文章放在服务器上的一个HTML文件中,每个文件都带有一个div,就像这样(假设文件名为myArticles.html,并且位于与您的文件夹相同的文件夹中主页):

< div id ='article1'> < b>第1条< / b>这是我的第一篇文章< / div> < div id ='article2'> < b>第2条< / b>这是我的第二篇文章< / div>

然后使用jQuery的 .load 函数加载这个数据使用AJAX进入你的页面。

假设你的文章1的按钮有id ='articleButton1'和2有id ='articleButton2'(两者都有href = '#',所以页面不会改变),那么这个jQuery就可以工作: $ $ $ $ $''#articleButton1')。点击(函数(){ $('#content')。load('myArticles.html#article1'); } $('#articleButton2')。点击(function(){ $('#content')。load('myArticles.html#article2'); }

这会从服务器上的该页面中选取正确的div,并将其放入 #content div

Basically I have a web page that has a div id="content" and a div id="sidebar",

What I would like to do is change the content in the content div when a link/button is clicked in the sidebar without having a serperate page for each article.

The reason being;

I am doing a BIO page for some multiple bands and there would be multiple buttons for each band then when a user clicks the button/link the content would change.

Any idea's, I have been looking into .click using jQuery or maybe PHP but not exactly sure on how to implement it.

解决方案

You could put each of the articles in one HTML file on your server, each with a div wrapped around it, like this (let's say the file is called myArticles.html and is in the same folder as your main page):

<div id='article1'> <b>ARTICLE 1</b> This is my first article </div> <div id='article2'> <b>ARTICLE 2</b> This is my second article </div>

Then use jQuery's .load function to load this data using AJAX into your page.

Say your button for article 1 has the id = 'articleButton1' and 2 has id = 'articleButton2' (and both with href='#', so the page doesn't change) then this jQuery would work:

$('#articleButton1').click(function(){ $('#content').load('myArticles.html #article1'); } $('#articleButton2').click(function(){ $('#content').load('myArticles.html #article2'); }

This picks the correct div out of that page on your server and puts it into the #content div

更多推荐

点击链接/按钮时更改div的内容

本文发布于:2023-06-09 04:14:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   链接   内容   div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!