将文本从大写更改为小写jQuery

时间:2016-11-22 13:19:39

标签: javascript jquery html

我正在尝试将文本从大写更改为小写但不起作用

这是我试过的代码

HTML结构

<div class="brand_name">
    <a href="#"> A-RET-TZ 0.1 %</a>
</div>

JS我试过

$('.brand_name').val().toLowerCase();

我遇到的问题是页面上的文本默认使用大写生成

任何人请建议

5 个答案:

答案 0 :(得分:8)

这也可以使用纯CSS来实现。

div.brand_name > a {
  text-transform: lowercase;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="brand_name">
  <a href="#"> A-RET-TZ 0.1 %</a>
</div>

答案 1 :(得分:5)

使用带有回调函数的text()方法根据旧内容更新文本内容。

&#13;
&#13;
$('.brand_name a').text(function(i, oldText) {
  return oldText.toLowerCase();
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="brand_name">
  <a href="#"> A-RET-TZ 0.1 %</a>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:2)

您需要设置anchor元素的文本,.text(fn)方法可以使用。

&#13;
&#13;
$('.brand_name a').text(function(i, text) {
  return text.toLowerCase();
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="brand_name">
  <a href="#"> A-RET-TZ 0.1 %</a>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

这是一个普通的javascript方式,有点多样化很好

document.querySelector(".brand_name a").innerHTML = document.querySelector(".brand_name a").innerHTML.toLowerCase();
<div class="brand_name">
  <a href="#"> A-RET-TZ 0.1 %</a>
</div>

答案 4 :(得分:0)

使用text属性fiddle

JS:

var $this = $('.brand_name a');
$this.text($this.text().toLowerCase());