如何使用javascript变量设置属性

时间:2013-09-23 13:46:36

标签: javascript razor

以下是我在durandal应用程序中的cshtml代码

<script type="text/javascript" src="~/Scripts/require.js" 
  id="countryscript" data-main="**here i have to set value**"></script>

我想用我的javascript变量值设置脚本属性 data-main 。怎么做到这一点?

我试过

document.getElementById("countryscript").data-main = countrycode;

= 符号附近显示语法错误。需要帮助..

2 个答案:

答案 0 :(得分:5)

试试这个:

document.getElementById("countryscript").setAttribute("data-main", countrycode);

答案 1 :(得分:3)

取自MDN

var d = document.getElementById("countryscript"); 
d.setAttribute("data-main", countrycod);

或者如果你有JQuery,这就容易多了

$('#countryscript').attr("data-main", countrycod);