我尝试了jquery教程,但它没有给出预期的输出

I try jquery tutorial, but it does not give the expected output

本文关键字:输出 jquery 教程      更新时间:2023-09-26

此程序将弹出一个警告消息,当用户点击显示的文本,然后使文本大写。

<script src="jquery-min.js"></script>
<script>
   //run once the page is loaded
   jquery(document).ready(function ($) {
       //Attach a click event to the span element
       $('#test_span').click(function () {
          //Read the elemnt's current value
          var elemnt_text = $('#test_span').html();
          //display the curretn value of this span
          alert(element_text);
          //convert the value to upper case
          element_text = element_text.toUpperCase();
          //change the span to this new value
          $('#test_span').html(element_text);
       });
  });
</script>
</head>
<span id='test_span'>Testing jQuery</span>

试试这个,elemnt_text拼写不正确。使用 element_text

获取元素的文本内容,使用.text()代替.html()

var element_text = $('#test_span').text();

代替

 var elemnt_text = $('#test_span').html();
演示:

http://jsfiddle.net/rk72eu8m/

jQuery text()和html()函数的区别

打字错误:

var elemnt_text = $('#test_span').html();

变量拼写错误。应该是element_text

应该可以正常工作

将代码更改为demo

var elemnt = $('#test_span');
//Attach a click event to the span element
elemnt.click(function () {
    //Read the elemnt's current value
    var element_text = elemnt.text();
    //display the curretn value of this span
    alert(element_text);
    //convert the value to upper case
    element_text = element_text.toUpperCase();
    //change the span to this new value
    $('#test_span').html(element_text);
});

你有拼写错误。试试这个

 <script src="jquery-min.js"></script>
    <script>
        //run once the page is loaded
        $(document).ready(function () {
            //Attach a click event to the span element
            $('#test_span').click(function () {
                //Read the elemnt's current value
                var element_text = $('#test_span').html();
                //display the curretn value of this span
                alert(element_text);
                //convert the value to upper case
                element_text = element_text.toUpperCase();
                //change the span to this new value
                $('#test_span').html(element_text);
            });
        });
</script>
    </head>
        <span id='test_span'>Testing jQuery</span>   

希望能有所帮助。