Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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
在html页面中编写javascript_Javascript_Jquery_Html - Fatal编程技术网

在html页面中编写javascript

在html页面中编写javascript,javascript,jquery,html,Javascript,Jquery,Html,我发现JavaScript我喜欢使用,但我需要它写在html页面,而不是作为一个单独的文件。。。这是一个 以下是我试图写它的方式: p span{color:blue;} 索尼传感器2.1mp $('p').html(函数(索引、值){ 返回值。替换(/(\d+)/g,$1'); }); 两件事,使用。并添加对jQuery库的引用 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js

我发现JavaScript我喜欢使用,但我需要它写在html页面,而不是作为一个单独的文件。。。这是一个

以下是我试图写它的方式:


p span{color:blue;}
索尼传感器2.1mp

$('p').html(函数(索引、值){ 返回值。替换(/(\d+)/g,$1'); });
两件事,使用
。并添加对jQuery库的引用

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>


并将您的脚本放入
$(document).ready()。

我想您真的应该看看

更新

当您使用jquery时,您需要将脚本添加到页面中,并且所有jquery代码都必须位于
$(document.ready(function(){})
中,如下所示

<head>
<style>
p span {color:blue;}
</style>
</head>

<body>

<p>sony sensor 2.1mp</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function() { 
    $('p').html(function(index, value) {
        return value.replace(/(\d+)/g, '<span>$1</span>');
    });
})
</script>

</body>

p span{color:blue;}
索尼传感器2.1mp

$(文档).ready(函数(){ $('p').html(函数(索引、值){ 返回值。替换(/(\d+)/g,$1'); }); })
它现在“工作”了。但是,您并没有指出它如何不做您希望它做的事情。所以,没有人能帮你做你想做的事。你想用$来显示数字吗?它是什么意思“把你的脚本放在$(document.ready()中”?“$(document).ready().”在哪里?是的!我找到了。。。非常感谢。在这种特定情况下,document.ready不是必需的,因为脚本位于它所针对的
元素之后。