Javascript 如何在html中输出javascript变量的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10107995/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:52:08  来源:igfitidea点击:

How to output a javascript variable's value inside html?

javascriptjqueryhtml

提问by David R

All,

全部,

I've a javascript routine which returns a string value, I want to use this routine inside my html tags like this,

我有一个返回字符串值的 javascript 例程,我想在这样的 html 标签中使用这个例程,

<a href="#"><script>getKeyValue('empDesignation')</script></a>

Is this possible?. Any insights?

这可能吗?。任何见解?

采纳答案by Yorgo

no you cant use javascript variables inside html tags. You have to set the value to html by using DHTML.

不,您不能在 html 标签内使用 javascript 变量。您必须使用 DHTML 将该值设置为 html。

If you post some code i can help you

如果您发布一些代码,我可以帮助您

回答by Sam

<html>
  <head>

<script type="text/javascript">
function alterText(){
document.getElementById('hello').innerHTML = empDesignation();
}
  function empDesignation(){   
    var output = 'test'
   return output
  }
</script>

  </head>
  <body onload="javascript:alterText()">

    <a id="hello"></a>

  </body>


  </html>

This should work. I had to mock up your empDesignation function.

这应该有效。我不得不模拟你的 empDesignation 函数。

回答by Dave

document.write(getKeyValue('empDesignation')); This would probably do the trick.

document.write(getKeyValue('empDesignation')); 这可能会奏效。

UPDATE: It has to be enclosed within <script>tags

更新:它必须包含在<script>标签中

回答by Nicola Peluchetti

You can do

你可以做

var txt = getKeyValue('empDesignation');
$('#result').text(txt);

This assumes that getKeyValue() returns a string. Then you put that the result as text of element with id=result

这假设 getKeyValue() 返回一个字符串。然后你把结果作为 id=result 元素的文本

EDIT - after your edit. you could do

编辑 - 在您编辑之后。你可以

HTML

HTML

<a href="#" data-key='empDesignation' class='functional'>your text</a>

js

js

//when you click on a link that has the class functional    
$('a.functional').on('click', function(e){
  //prevent the default action
  e.preventDefault();
  //retrieve the data that from the html5 data attribute and pass it to 
  //your custom function
  var result = getKeyValue($(this).data('key'));
  //use the resulting string as the new text for the clicked link
  $(this).text(result);
})

回答by srini

inside html tags this getKeyValue('empDesignation') it is not possible,

在 html 标签中,这个 getKeyValue('empDesignation') 是不可能的,

use innerHTML like below example

像下面的例子一样使用innerHTML

           <html>
    <script type="text/javascript">
    function changeText(){
        document.getElementById('boldStuff').innerHTML = 'is Software Developer';
    }
    </script>
    <body>
    <p>Employee Designation <b id='boldStuff'>is what?</b> </p> 
    <input type='button' onclick='changeText()' value='Change Text'/>
    </body>
    </html>

EDIT

编辑

          <script type="text/javascript">
    function changeText(){
             var get_des=getKeyValue('empDesignation');

            document.getElementById('change_id').innerHTML = get_des;
    }
    </script>

           <a href="#" id="change_id" onclick='changeText()'></a>