Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript变量可以在普通HTML中使用吗?_Javascript_Arrays_Variables - Fatal编程技术网

JavaScript变量可以在普通HTML中使用吗?

JavaScript变量可以在普通HTML中使用吗?,javascript,arrays,variables,Javascript,Arrays,Variables,我的意思是,在-标记之外,可以在HTML中使用声明和初始化的变量/数组吗?外汇 <script type="text/javascript"> var foo = array('placeholder1', 'placeholder2'); </script> <body> <p><!--access the variable here-->foo[0]</p> </body> var foo=array(

我的意思是,在
-标记之外,可以在HTML中使用声明和初始化的变量/数组吗?外汇

<script type="text/javascript">
var foo = array('placeholder1', 'placeholder2');
</script>

<body>
<p><!--access the variable here-->foo[0]</p>
</body>

var foo=array('placeholder 1','placeholder 2');
富[0]

在这种情况下,如何访问变量/数组?像这样:

<p><script type="text/javascript">document.print(foo[0])</script></p>
文档打印(foo[0])


有两种方法可以做到这一点。这是更好的一个:

<script type="text/javascript">
// make sure to do this onLoad, for example jQuery's $()
var foo = array('placeholder1', 'placeholder2');
document.getElementById("fooHolder").innerHTML = foo.toString();
</script>
...
<p id="fooHolder"></p>

//确保进行这种onLoad,例如jQuery的$()
var foo=array('placeholder 1','placeholder 2');
document.getElementById(“fooHolder”).innerHTML=foo.toString();
...

或者您可以这样做(正如Marcel指出的,这在XHTML中不起作用,而且无论如何也不应该使用):

文档写入(foo)


您可以执行以下操作:

<script>
  var str = 'hello there';
  document.getElementById('para').innerHTML = str;
</script>

var str='你好';
document.getElementById('para')。innerHTML=str;
其中元素具有指定的id:

<p id="para"></p>


这是您在页面其他位置访问它的唯一直接方式。打开另一个脚本标记并打印它


您还可以使用诸如innerHTML之类的方法将值放在某个地方。

我认为您无法从html访问javascript,但您可以通过javascript设置dom对象的innerHTML,因此您可能需要换一种方式。第一次谷歌搜索我发现,所以我不能保证它的好,但它有一个快速的样本


您无法访问脚本标记之外的javascript变量,因为

  • Html不识别任何变量,它只是呈现支持的Html元素
  • 变量用于存储临时变量,也就是动态数据,如果您想要更动态的数据,那么可以使用PHP

  • 不必要的冗长,但使用标准DOM方法

    <script>
        window.onload = function(){
            // you do not need to initialize like this, but I like to
            var bar1 = new String('placeholder1');
            var bar2 = new String('placeholder2');
            var foo = new Array();
    
            // populate the Array with our Strings 
            foo.push(bar1);
            foo.push(bar2);
    
            // create an array containing all the p tags on the page 
            // (which is this case is only one, would be better to assign an id)
            pArray = document.getElementsByTagName('p');
    
            // create a text node in the document, this is the proper DOM method
            bar1TextNode = document.createTextNode(foo[0].toString());
    
            // append our new text node to the element in question
            pArray[0].appendChild(bar1TextNode);
        };
    </script>
    
    <body>
        <p></p>
    </body>
    
    
    window.onload=函数(){
    //你不需要像这样初始化,但我喜欢这样
    var bar1=新字符串(“占位符1”);
    var bar2=新字符串(“占位符2”);
    var foo=新数组();
    //用字符串填充数组
    foo.push(bar1);
    foo.push(bar2);
    //创建一个包含页面上所有p标记的数组
    //(这种情况下只有一个,最好分配一个id)
    pArray=document.getElementsByTagName('p');
    //在文档中创建文本节点,这是正确的DOM方法
    bar1TextNode=document.createTextNode(foo[0].toString());
    //将新文本节点附加到相关元素
    pArray[0]。appendChild(bar1TextNode);
    };
    


    您甚至可以使用AngularJS表达式

    <html>
         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
         <script>
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function($scope) {
                $scope.framework= "AngularJS";
            });
        </script>
    
        <body>
    
            <div ng-app="myApp" ng-controller="myCtrl">
                <p>I want to use variables directly in HTML using: {{ framework }}</p>
            </div>
    
        </body>
    </html>
    
    
    var-app=angular.module('myApp',[]);
    应用程序控制器('myCtrl',函数($scope){
    $scope.framework=“AngularJS”;
    });
    我想直接在HTML中使用变量:{{framework}


    上面的代码将打印出“我想使用:AngularJS在HTML中直接使用变量”。您可以使用大括号来编写AngularJS表达式。例如:{{expression}.

    document.write
    在XHTML中不起作用:这是一个设置HTML标记以包含JavaScript变量内容的好例子。但它不允许您在HTML中指定一个JavaScript变量来自动返回JavaScript变量的内容。正确的答案是,所要求的是不能做的,但这里有一个替代方法。
    <html>
         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
         <script>
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function($scope) {
                $scope.framework= "AngularJS";
            });
        </script>
    
        <body>
    
            <div ng-app="myApp" ng-controller="myCtrl">
                <p>I want to use variables directly in HTML using: {{ framework }}</p>
            </div>
    
        </body>
    </html>