JavaScript passing a string into a method, which is an a attribute
I have the following code:
myFunction('test')
I wish to set this on the onclick of an html element
<a href=开发者_如何转开发"#" onclick="myFunction('test')" >Link</a>
I need to capture this as a string and add it via JavaScript as an inner element. How can I capture this?
"<a href="#" onclick="myFunction('test')" >Link</a>"
'<a href="#" onclick="myFunction('test')" >Link</a>'
Neither seem to work.
Do you mean you want to capture 'test' as a string, and pass it as the parameter? Try using backslahes before the single quotes surrounding test
<a href="#" onclick="myFunction(\'test\')" >Link</a>
Something like...
HTML:
<a href="#" onclick="myFunction('test')" >Link</a>
SCRIPT:
<script type="text/javascript">
function myFunction(userInput){
document.getElementById('myNewHTMLElementID').innerHTML = userInput;
}
</script>
i think
myFunction('test')
should be
myFunction(test)
if you want to use test as variable in the function.
The function call itself looks OK.
精彩评论