简体   繁体   English

使用jQuery将文本添加到div

[英]Adding text to a div using jQuery

I am trying to do something very simple but somehow its not working. 我想做一些非常简单的事情但不知何故它不起作用。

I have 4 divs on the right, and one blank div on the left. 我右边有4个div,左边有一个空白div。 I want the left div to show certain text, depending on which div I hovered on, on the right. 我希望左边的div显示某些文字,这取决于我在右侧徘徊的div。

It's very simple, but somehow I am missing something. 这很简单,但不知怎的,我错过了一些东西。

<div class="col-md-6 col-sm-12 col-xs-12 Left">
    <h3>left:</h3>
    <div id="roles">
       <button class="role" id="role_1">
           Role_1
       </button>
       <button class="role" id="role_2">
           Role_2
       </button>
       <button class="role" id="role_3">
           Role_3
       </button>
       <button class="role" id="role_4">
           Role_4
       </button>   
    </div>
</div>
<div class="col-md-6 col-sm-12-col-xs-12 right">
    <div id="right">
    </div>
</div>

Here is my jQuery: 这是我的jQuery:

$(function(){
    $('#role_1').hover(function(){
        $('#right').html('<p>Text_1</p>')
    });
     $('#role_2').hover(function(){
        $('#right').html('<p>Text_2</p>')
    });
     $('#role_3').hover(function(){
        $('#right').html('<p>Text_3</p>')
    });
     $('#role_4').hover(function(){
        $('#right').html('<p>Text_4</p>')
    })

})

This is just a basic code to write "Text" when it hovers over #role_1. 这只是在#rol​​e_1上悬停时编写“Text”的基本代码。 Somehow it doesn't work. 不知怎的,它不起作用。 It works on JSFiddle 它适用于JSFiddle

Here is my CDN: 这是我的CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.js"></script>

EDIT: It works on JSFiddle. 编辑:它适用于JSFiddle。 Somehow it is not working on http://hellosimpletax.com/#/ 不知怎的,它无法在http://hellosimpletax.com/#/上运行

JSFiddle 的jsfiddle

Thank you! 谢谢!

Check your console you will find error 检查您的控制台,您会发现错误

Uncaught SyntaxError: Unexpected token ILLEGAL 未捕获的SyntaxError:意外的标记ILLEGAL

Try this you just forget to close #roles selector 试试这个你忘了关闭#roles选择器

$('#roles').mouseleave(function(){ $('#right').html('') }) 
---------^

Working Demo 工作演示

Update 更新

OP got it working by moving his code into the Angular controller as @krishna suggested in a comment after we noticed that the view wasn't full loaded on document ready. OP通过将他的代码移动到Angular控制器中来实现它的工作,因为@krishna在评论中建议我们注意到视图未在文档就绪时完全加载。

Try this: 尝试这个:

Working Fiddle: http://jsfiddle.net/tracyfu/twsL2fgq/6/ 工作小提琴: http//jsfiddle.net/tracyfu/twsL2fgq/6/

$(function(){
  $('#dev').on('mouseenter', function(){
    $('#right').html('<p>Text_1</p>');
  });
  $('#des').on('mouseenter', function(){
    $('#right').html('<p>Text_2</p>');
  });
  $('#eng').on('mouseenter', function(){
    $('#right').html('<p>Text_3</p>');
  });
  $('#art').on('mouseenter', function(){
    $('#right').html('<p>Text_4</p>');
  })
  $('#roles').on('mouseleave', function(){
    $('#right').html('');
  })     
})

A couple unrelated asides: 一对无关的旁白:

  • If you specify col-md-6 , you shouldn't need to declare col-xs-12 since Bootstrap treats mobile as its baseline and should automatically stack your columns if you're using container-fluid 如果指定col-md-6 ,则不需要声明col-xs-12因为Bootstrap将mobile视为其基线,如果您使用的是container-fluid ,则应自动堆叠列
  • The preferred way to attach event handlers with jQuery is by using on() 使用jQuery附加事件处理程序的首选方法是使用on()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM