简体   繁体   English

如何使用jQuery更新p的值(段落标签)

[英]How to update a value of p (Paragraph tag) using jquery

I want to update the value of the P tag on change of drop down menu using jquery. 我想使用jquery在更改下拉菜单时更新P标签的值。 My code for changing and getting value using jquery is as follows. 我使用jquery更改和获取价值的代码如下。 Please give some solutions thanks... 请提供一些解决方案,谢谢...

<script type="text/javascript">
(document).ready(function(){
  $('.dropdown-menu li a').click(function(){
    alert($(this).text());

  });
});
</script>

 <p id="qtitle" style="padding-left:15px;"><strong>text to change</strong></p>

Try this: 尝试这个:

$(function(){
    $('.dropdown-menu li a').click(function(){
        $("#qtitle strong").html($(this).text());
    });
});

HTML: HTML:

<p id="qtitle" style="padding-left:15px;"><strong>text to change</strong></p>

<div id="myList" class="dropdown-menu">
    <li><a>1</a></li>
    <li><a>2</a></li>
    <li><a>3</a></li>
    <li><a>4</a></li>
</div>

JS: JS:

 $('.dropdown-menu li a').click(function(){
    $('#qtitle strong').text($(this).text());
 });

.html(text) works, too. .html(text)也可以。 However .text(text) is a secure way to alter the text value of elements. 但是,.text(text)是更改元素的文本值的安全方法。 Check out the doc at http://api.jquery.com/text/ http://api.jquery.com/text/上查看文档

See the working code at: 请参阅以下工作代码:

JSFiddle JSFiddle

Please replace your code as 请替换为

  document.ready(function(){
      $('.dropdown-menu li a').click(function(){
        $("#qtitle strong").html($("#YourNewText").val());
      });
    });

HTML Code: HTML代码:

<select class="target">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<p id="qtitle" style="padding-left:15px;"><strong>Text to change</strong></p>

JQuery Code: jQuery代码:

$( ".target" ).change(function() {
    $("#qtitle strong").html($(".target option:selected").text());
});

Demo in here jsfiddle. 演示在这里 jsfiddle。

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

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