Javascript根据Value切换

时间:2014-04-05 16:17:05

标签: javascript jquery toggle

我有一个元素:

<a href="#" type="link" class="button send" classAct="button send" classSel="button send pressed" label="encrypt" title="sendmessage" onclick="add_encryption();">Encrypt</a>

当通过以下javascript切换时,我希望它显示为:

<a href="#" type="link" class="button send" classAct="button send" classSel="button send pressed" label="decrypt" title="sendmessage" onclick="add_encryption();">Decrypt</a>

<script type="text/javascript">

function add_encryption() {
    document.getElementById('compose-subject').value = "[encrypt]";
}
</script>

问题是我不知道如何为它编写切换。

2 个答案:

答案 0 :(得分:0)

这样做:

<a href="#" id="toggle" type="link" class="button send" classAct="button send" classSel="button send pressed" label="encrypt" title="sendmessage" onclick="encryptdecrypt();">Encrypt</a>


<script type="text/javascript>

function encryptdecrypt() {

    if(document.getElementById('toggle').innerHTML == 'Encrypt')
{
    document.getElementById('compose-subject').value = "[encrypt]";
    document.getElementById('toggle').innerHTML == 'Decrypt';
}
else
{
// decrypt here
document.getElementById('compose-subject').value = "[decrypt]";
document.getElementById('toggle').innerHTML == 'Encrypt';
}
}

</script>

答案 1 :(得分:0)

由于您要求切换<a>代码,此处是代码

<a href="#" id="toggle" type="link" class="button send" classact="button send" classsel="button send pressed" label="encrypt" title="sendmessage" onclick="encryptdecrypt(this);">Encrypt </a>


<script type="text/javascript">

    function encryptdecrypt(obj) {
        obj.innerText = obj.innerText == "Encrypt" ? "Decrypt" : "Encrypt";
    }

</script>

我无法理解&#39;&#39;撰写主题&#39;&#39;假设它是页面中的另一个元素,你可以使用innerText和getElementById来改变它的值