I have this snippet that works perfect, but when i put it on Dreamweaver it doesn't. What is missing?
我有这个完美的片段,但是当我把它放在Dreamweaver上时却没有。缺什么?
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace('(Código:','');
$(this).html(newText);
});
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace(')','');
$(this).html(newText);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
(Código: 40398488494)
</div>
Any help i would appreciate, thanks
任何帮助,我将不胜感激,谢谢
2 个解决方案
#1
0
It's better if you load the jquery in the header like that.
如果你像这样在标题中加载jquery会更好。
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
#2
0
At the time of initial page load, you're searching for a (then) unknown element, because the JS is firing before the HTML has been rendered in full.
在初始页面加载时,您正在搜索(然后)未知元素,因为JS在HTML完全呈现之前触发。
You can either place the JS at the bottom of the page, just before the closing </body>
, and put the <div><small>(Código: 40398488494)</small></div>
above/before the JS, or you can (preferably) use $(document).ready()
.
您可以将JS放在页面底部,就在结束 之前,然后在JS之前/之前放置
Each of the following will work:
以下各项均有效:
With $(document).ready()
<!doctype html>
<html>
<head></head>
<body>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script>
$(document).ready(function() {
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace('(Código:','');
$(this).html(newText);
});
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace(')','');
$(this).html(newText);
});
});
</script>
<div><small>(Código: 40398488494)</small></div>
</body>
</html>
Notice how it doesn't matter where <div><small>(Código: 40398488494)</small></div>
is because the JS will not fire until the page is ready.
注意
WITHOUT $(document).ready()
<!doctype html>
<html>
<head></head>
<body>
<div><small>(Código: 40398488494)</small></div>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script>
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace('(Código:','');
$(this).html(newText);
});
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace(')','');
$(this).html(newText);
});
</script>
</body>
</html>
<div><small>(Código: 40398488494)</small></div>
must come before your JS if you're not using $(document).ready()
.
如果你没有使用$(document).ready(),那么
The first example (with $(document).ready()
is preferable as it allows you to customize what JS fires and when.
第一个示例(使用$(document).ready()是首选,因为它允许您自定义JS触发和何时触发。
More on .ready()
更多关于.ready()
#1
0
It's better if you load the jquery in the header like that.
如果你像这样在标题中加载jquery会更好。
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
#2
0
At the time of initial page load, you're searching for a (then) unknown element, because the JS is firing before the HTML has been rendered in full.
在初始页面加载时,您正在搜索(然后)未知元素,因为JS在HTML完全呈现之前触发。
You can either place the JS at the bottom of the page, just before the closing </body>
, and put the <div><small>(Código: 40398488494)</small></div>
above/before the JS, or you can (preferably) use $(document).ready()
.
您可以将JS放在页面底部,就在结束 之前,然后在JS之前/之前放置
Each of the following will work:
以下各项均有效:
With $(document).ready()
<!doctype html>
<html>
<head></head>
<body>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script>
$(document).ready(function() {
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace('(Código:','');
$(this).html(newText);
});
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace(')','');
$(this).html(newText);
});
});
</script>
<div><small>(Código: 40398488494)</small></div>
</body>
</html>
Notice how it doesn't matter where <div><small>(Código: 40398488494)</small></div>
is because the JS will not fire until the page is ready.
注意
WITHOUT $(document).ready()
<!doctype html>
<html>
<head></head>
<body>
<div><small>(Código: 40398488494)</small></div>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script>
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace('(Código:','');
$(this).html(newText);
});
$('div').each( function() {
orgText = $(this).html();
newText = orgText.replace(')','');
$(this).html(newText);
});
</script>
</body>
</html>
<div><small>(Código: 40398488494)</small></div>
must come before your JS if you're not using $(document).ready()
.
如果你没有使用$(document).ready(),那么
The first example (with $(document).ready()
is preferable as it allows you to customize what JS fires and when.
第一个示例(使用$(document).ready()是首选,因为它允许您自定义JS触发和何时触发。
More on .ready()
更多关于.ready()