admin管理员组文章数量:1327750
Is it possible in jquery to insert text after a span. For example, I would like to change the price value after the dollar sign below.
<div id="price">
<span>$</span>
10.00
</div>
The only way I can see how to do this, is to remove the span element (store as variable), add the price value, and then append the currency value again. Seems pretty hacky.
Is it possible in jquery to insert text after a span. For example, I would like to change the price value after the dollar sign below.
<div id="price">
<span>$</span>
10.00
</div>
The only way I can see how to do this, is to remove the span element (store as variable), add the price value, and then append the currency value again. Seems pretty hacky.
Share Improve this question asked Feb 11, 2016 at 9:34 Oliver WatkinsOliver Watkins 13.5k39 gold badges134 silver badges250 bronze badges 5- 1 You can do it by accessing (and modifying the value of) the textnode holding the amount, like shown here: jsfiddle/7mdygLLs – techfoobar Commented Feb 11, 2016 at 9:42
- why is this down voted, and voted to close? – Oliver Watkins Commented Feb 11, 2016 at 10:01
- I wonder why too. Can't find anything wrong with the question at all. – techfoobar Commented Feb 11, 2016 at 10:06
- @Oliver Watkins: that DOM looks kinda quirky – Gal Margalit Commented Feb 11, 2016 at 10:26
- Whoever down voted this question please specify reason so that questioner can correct his mistake from next time. – foxt7ot Commented Feb 11, 2016 at 10:28
8 Answers
Reset to default 2You can change the price using lastChild
and nodeValue
like following.
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="price">
<span>$</span>
10.00
</div>
<script>
var price = $('#price')[0];
price.lastChild.nodeValue = "155.00";
</script>
Yes you can do it as:
$("#price").contents(":not(span)").text("Text");
Just add another span to wrap around the price value.
<div id="price">
<span>$</span>
<span id="price-value">10.00</span>
</div>
Then you can $("#price-value")
to manipulate that part on its own.
One another way
<script src="http://code.jquery./jquery.min.js"></script>
<div id="price">
<span>$</span>
10.00
</div>
<script type="text/javascript">
$('#price').contents().last()[0].textContent=' 20.00';
</script>
As a much easier alternative, one might also use CSS to prepend the currency symbol. This avoids using the span altogether and allows changing the currency symbol.
CSS Examples:
.currency div::before { content: "$" }
.currency.yen div::before { content: "¥" }
And a working code snippet:
$('#item1').html('10.00');
$('#item2').html('8.80');
$('#item3').html( '20.30');
.currency.dollar div::before { content: "$" }
.currency.pound div::before { content: "£" }
.currency {
font-family: monospace;
font-size: 24px;
font-weight: bold;
border: 1px lightgray solid;
width: 8em;
padding: 4px;
text-align: right;
background-color: ghostwhite;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="currency dollar">
<div id="item1"></div>
<div id="item2"></div>
<div id="item3"></div>
</div>
try this
$("#pri").text("100");
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="price">
<span id='si'>$</span>
<span id='pri'>10.00</span>
</div>
Setting the text need some old fashioned JavaScript assistance. Using textContent
.
$('#price').contents().filter(function() {
return this.nodeType == 3;
}).last()[0].textContent = 'changed';
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="price">
<span>$</span>
10.00
</div>
You can try insertAfter() in jQuery.
HTML:
<button>Insert span element after span element</button>
<br>
<div id="price">
<span>$</span>
</div>
jQuery:
$(document).ready(function(){
$("button").click(function(){
$("<span>100</span>").insertAfter("span");
});
});
本文标签: javascriptChanging text after a spanStack Overflow
版权声明:本文标题:javascript - Changing text after a span - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742230568a2437146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论