Javascript - 删除线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9375445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - strikethrough
提问by Mert MET?N
i try to make text strikethrough with javascript. I know nothing about Javascript and try to search on the net how to that.
我尝试使用 javascript 制作文本删除线。我对 Javascript 一无所知,并尝试在网上搜索如何做到这一点。
<script language="JavaScript">
function recal(val,sum)
{
if(sum == true)
{
var total = parseInt(document.getElementById("total").innerHTML, 10);
total+=val;
document.getElementById("total").innerHTML=total;
}
else
{
var total = parseInt(document.getElementById("total").innerHTML, 10);
total-=val;
document.getElementById("total").innerHTML=total;
var pnl = document.getElementById("totalEvents");
}
var pnl = document.getElementById("totalEvents");
var pnl2 = document.getElementById("eventCategory");
var pnl3 = document.getElementById("nameID");
**strikethrough starts here**
if (!sum && pnl.firstChild.tagName != "S" && pnl2.firstChild.tagname !="S")
{
pnl.innerHTML = "<S>"+ pnl.innerHTML+"</S>";
pnl2.innerHTML = "<S>"+ pnl2.innerHTML+"</S>";
}
else
{
pnl.innerHTML = pnl.firstChild.innerHTML;
pnl2.innerHTML = pnl2.firstChild.innerHTML;
}
}
</script>
it makes textstrikethrough but something is wrong. Even if i choose second checkbox it affects first checkbox why :(
它使 textstrikethrough 但有些地方是错误的。即使我选择第二个复选框它会影响第一个复选框为什么:(
http://jsfiddle.net/aHH9w/(my full html page)
http://jsfiddle.net/aHH9w/(我的完整 html 页面)
回答by Rohan Prabhu
You are peforming a pretty convoluted way of achieving this, something that can actually be quite easily done. If you have an HTML element, say with the id 'myelement':
您正在以一种非常复杂的方式实现这一目标,实际上可以很容易地做到这一点。如果你有一个 HTML 元素,用 id 'myelement' 说:
<div id="myelement">Hello world</div>
To create a strikethrough, all you need to do in JS is:
要创建删除线,您需要在 JS 中做的是:
var ele = document.getElementById("myelement");
ele.style.setProperty("text-decoration", "line-through");
If you need to check if there is a strikethrough on an element:
如果您需要检查元素上是否有删除线:
var ele = document.getElementById("myelement");
var isStruck = (ele.style.getProperty("text-decoration") == "line-through");
Although this is not really recommended. Use an internal variable to keep track of state.
虽然这不是真的推荐。使用内部变量来跟踪状态。