如何使用JavaScript修改基于onclick的文本?
Good evening everybody!
大家晚上好!
Well, I am studying JavaScript at the moment, and I reachead the point in where I need, based on clicked text (button, ... it doesn't matter, I guess).
嗯,我现在正在学习JavaScript,并且根据点击的文本(按钮,……)没关系,我猜)。
For example:
例如:
This message should be edited.
这个消息应该被编辑。
Two choices: ES - EN
有两种选择:ES - EN
If choice "ES" is clicked/selected, then:
如果选择“ES”被点击/选择,则:
This message has been edited to ES.
此消息已编辑为ES。
Otherwise, if choice "EN" is clicked/selected, then:
否则,如果单击/选择“EN”,则:
This message has been edited to EN.
此消息已被编辑为EN。
I think this could have been achieved with an if in a script, but the problem is how to tell the script which option has been chosen, if "ES" or "EN".
我认为这可以通过脚本中的if实现,但是问题是如何告诉脚本选择了哪个选项,如果是“ES”或“EN”。
I have this:
我有这个:
<p id="demo">This message should be edited.<p>
<a href="#" id="ES" onclick="func();">ES</a> - <a href="#" id="EN" onclick="func();">EN</a> - <a href="#" id="JP" onclick="func();">JP</a>
And as I said, the problem is that I don't know how to tell the script what has been clicked/selected.
正如我所说的,问题是我不知道如何告诉脚本什么被单击/选择了。
Thank you for any help you may give me (if possible, an explanation with or over a solution would be amazing because I want to learn and understand, not to solve this problem without being able to solve the same thing or similar in a future).
谢谢你给我的任何帮助(如果可能的话,有一个或多个解决方案的解释将是令人惊讶的,因为我想要学习和理解,而不是在未来无法解决同样的问题或类似的问题的情况下解决这个问题)。
6 个解决方案
#1
1
You can pass a argument to the function for example
例如,可以将参数传递给函数
function fun(lang){
If(lang === "ES"){
Do something
}else If(lang === "EN"){
Do something
}
}
And when you call your function put something like this:
当你调用你的函数时可以这样写:
onClick="fun('ES')" or onClick="fun('EN')
onClick = "有趣(ES)”或onClick = "有趣(EN)
NOTE: instead of If 's you could use switch statement inside your function fun(lang)
注意:可以在函数fun(lang)中使用switch语句,而不是If 's
更多相关文章
- 通过脚本自动屏蔽非法IP(转http://bbs.5y6s.com/htm_data/21/080
- 在离开当前页面时调用函数。
- money_format()函数显示 而不是本地货币符号(磅)
- EL中的fn函数,jstl的fn函数,fn函数,fn函数描述,fn使用举例
- HTML5_旋转轮播-电子相册-新startMove函数写无缝轮播
- 深入剖析php执行原理(4):函数的调用
- php中的gethostbyname函数有问题
- JRoute::_() -函数在哪里?在joomla
- PHP通过系统命令执行Python脚本