如何使用jquery仅更改某个字符
切换时,我需要将+更改为-。代码设置在 HTML 你可以这样做:如何使用jquery仅更改某个字符,jquery,Jquery,切换时,我需要将+更改为-。代码设置在 HTML 你可以这样做: $("a.expandB").click(function() { var $a = $(this); var $h3 = $a.find('h3'); $(this).toggleClass("expandB").next().slideToggle("slow", function() { va
$("a.expandB").click(function() {
var $a = $(this);
var $h3 = $a.find('h3');
$(this).toggleClass("expandB").next().slideToggle("slow", function() {
var text = $h3.text();
$a.hasClass('expandB') ? $h3.text(text.replace('-', '+')) : $h3.text(text.replace('+', '-'));
});
// Toggles the text from expand to Collapse
return false; //Prevent the browser jump to the link anchor
});
您的问题与此非常相似,因此您的答案也可能相似:
不需要额外的课程。只需通过.text进行交换
我在下面发布的链接是谷歌的第一个结果,如果不是正确的答案,它也包含一个类似的答案。谢谢gabe,它可以工作,但字体大小会改变。我如何确保h3仍然存在。我发现了最新情况。我添加了$text+text+;变量减速后。非常感谢。
$(document).ready(function(){
$('.content').hide();
$("a.expandB").click(function() {
$(this).toggleClass("expandB").next().slideToggle("slow", function(){
// script to toggle between + and -
});
return false; //Prevent the browser jump to the link anchor/Prevent the browser jump to the link anchor
});
});
$("a.expandB").click(function() {
var $a = $(this);
var $h3 = $a.find('h3');
$(this).toggleClass("expandB").next().slideToggle("slow", function() {
var text = $h3.text();
$a.hasClass('expandB') ? $h3.text(text.replace('-', '+')) : $h3.text(text.replace('+', '-'));
});
// Toggles the text from expand to Collapse
return false; //Prevent the browser jump to the link anchor
});
$("a.expandB").click(function() {
$('span', this).text( $('span').text() == '+' ? '-' : '+');
$(this).toggleClass("expandB").next().slideToggle("slow");
return false;
});
$("a.expandB").click(function() {
var t=$(this);
t.toggleClass("expandB").next()
.slideToggle("slow", function(){
if(t.hasClass('expandB')) $('h3', t).html('Advocacy +');
else $('h3', t).html('Advocacy -');
});
return false;
});