使用Javascript功能设置标题属性 [英] Setting title attribute with Javascript function
本文介绍了使用Javascript功能设置标题属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到,如果我可以使用标签
元素的标题
属性值来传递值Javascript?
I am trying to find, if I can pass a value on the title
attribute of a label
element using Javascript?
我试过以下Javascript代码:
I have tried the following Javascript code:
function myFunction()
{
return "Hello!";
}
以及一段HTML代码:
And a piece of HTML code:
<label title="myFunction()">TEST</label>
但是,这不起作用。它只是将'myFunction()'显示为文本。
But, this isn't working. It just show the 'myFunction()' as a text.
有可能,我正在尝试做什么?如果是,那么正确的语法是什么?
Is it possible, what I am trying to do? If yes, what is the correct syntax?
推荐答案
<label id='mylabel'>TEST</label>
<script>
function myFunction() {
return "Hello!";
}
document.getElementById('mylabel').setAttribute('title', myFunction());
</script>
应该完成这项工作。它首先选择标签,然后设置属性。
Should do the job. It first selects the label and then sets the attribute.
这篇关于使用Javascript功能设置标题属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文