jquery - 如何在单击图像链接时更改 Div 的内容?

标签 jquery html css dynamic

我正在创建一个包含两列的布局。

其中一个包含图像和第二个内容。现在我想,每当用户单击图像时,第二列中的内容就会发生变化。

HTML

    <div class="col-sm-6">
            <div class="row" style="width: 75%;margin: 0 auto;">
             <div class="col-xs-6">
                <a href="#" class="p1"><span id="image1"><b>Planning</b></span></a>
             </div>
             <div class="col-xs-6">
                <a href="#" class="p2"><span id="image2"><b>Results</b></span></a>         
             </div>
           </div><br>
           <div class="row" style="width: 75%;margin: 0 auto;">
             <div class="col-xs-6">
                <a href="#" class="p3"><span id="image3"><b>Improvements</b></span></a>            
             </div>
             <div class="col-xs-6">
                <a href="#"  class="p4"><span id="image4"><b>Communication</b></span></a>              
             </div>
           </div>
<div class="st">
            <div class="planning">
            <h3>Planning</h3>           
            <p class="why-text">Search Marketing Group is a leader in creating unique Seo&nbsp;strategies that would help your website rank on page 1 of 
            Google for your most competitive keywords in your niche. We pride ourself on creating custom solutions for&nbsp;businesses and 
            making it work for them. </p>   
            </div>  

            <div class="results">
            <h3>Results</h3>            
            <p class="why-text">Search Marketing Group is a leader in creating unique Seo&nbsp;strategies that would help your website rank on page 1 of 
            Google for your most competitive keywords in your niche. We pride ourself on creating custom solutions for&nbsp;businesses and 
            making it work for them. </p>   
            </div>

            <div class="improvements">
            <h3>Improvements</h3>           
            <p class="why-text">Search Marketing Group is a leader in creating unique Seo&nbsp;strategies that would help your website rank on page 1 of 
            Google for your most competitive keywords in your niche. We pride ourself on creating custom solutions for&nbsp;businesses and 
            making it work for them. </p>   
            </div>

            <div class="communication">
            <h3>Communication</h3>          
            <p class="why-text">Search Marketing Group is a leader in creating unique Seo&nbsp;strategies that would help your website rank on page 1 of 
            Google for your most competitive keywords in your niche. We pride ourself on creating custom solutions for&nbsp;businesses and 
            making it work for them. </p>   
            </div>              
          </div>
    <div class="col-sm-6 how-text">

    </div>

CSS

.st { display:none; }
.how-text { min-height: 300px;
    height: auto;
    background-color: #e6ebe4; 
    padding: 20px;
    margin: 25px 0px 10px 0px;
    border-radius: 3px; }

Jquery

$(document).ready(function() {
   $('.p1').click(function(){
    $('.how-text').html($('.planning').html());
});

    $('.p2').click(function(){
    $('.how-text').html($('.results').html());
});

    $('.p3').click(function(){
    $('.how-text').html($('.improvements').html());
});

    $('.p4').click(function(){
    $('.how-text').html($('.communication').html());
});
});

尝试一下 Fiddle

https://jsfiddle.net/19suymt4/

最佳答案

已编辑

这是最简单的方法

getElementsByClassName

为了您的理解,我更改了您的代码

Working Example

关于jquery - 如何在单击图像链接时更改 Div 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560050/

相关文章:

javascript - 导出时重命名下载文件

php - 使用 php 获取 div 的 css 类

javascript - 单击 anchor 标记时获取父级 <th> 的属性值

php - 在 jquery 中使用 #urls

php - 数据未存储在 PHP 站点的 mysql 数据库表中

javascript - JQuery - 将数据从集合中的每个元素传递到回调

html - 未通过电子邮件 SQL 读取内联样式

html - 在鼠标悬停时展开 Material 扩展面板

javascript - 如何通过jsonp读取cookie?

javascript - Kendo UI - 将网格导出为没有页面 block 的 PDF