Javascript 粗体/未装订按钮
作为一个项目的一部分,我目前正在使用一个在线文本编辑器,至少可以说,我的粗体按钮有一个小问题 这是我的HTML按钮:Javascript 粗体/未装订按钮,javascript,html,Javascript,Html,作为一个项目的一部分,我目前正在使用一个在线文本编辑器,至少可以说,我的粗体按钮有一个小问题 这是我的HTML按钮: <button id="b" onclick="bold()">Bold</button> 如果我拿走了所有的东西,只留下: function bold() { document.getElementById("texto").style.fontWeight = 'bold'; } 它使我的文本加粗,但我的目标是,
<button id="b" onclick="bold()">Bold</button>
如果我拿走了所有的东西,只留下:
function bold() {
document.getElementById("texto").style.fontWeight = 'bold';
}
它使我的文本加粗,但我的目标是,当我第二次单击按钮时,能够在我的
中解除文本的绑定
我做错了什么?试试这个:
function Bold()
{
var text = document.getElementById("texto");
var weight = text.style.fontWeight;
if(weight == 'bold')
{
text.style.fontWeight = 'normal';
}
else
{
text.style.fontWeight = 'bold';
}
}
试试这个:
function Bold()
{
var text = document.getElementById("texto");
var weight = text.style.fontWeight;
if(weight == 'bold')
{
text.style.fontWeight = 'normal';
}
else
{
text.style.fontWeight = 'bold';
}
}
使用外部JavaScript,以便缓存它,以加快加载速度,因为作为最佳实践,您应该将HTML与JavaScript分开。但实际上,您的问题是每次调用函数时都在重新定义
var-ban
。观察和学习:
//<![CDATA[
// change the pre var on any page using this onload technique
var pre = onload, doc, bod, htm, E, boldToggle; // higher scope in case other onloads need access
onload = function(){ // same as window.onload - note that window is implicit
if(pre)pre(); // execute previous onload
doc = document, bod = doc.body, htm = doc.documentElement;
E = function(id){
return doc.getElementById(id);
}
boldToggle = (function(){ // Anonymous function executes and scopes off var b before returning an unexecuted function
var b = 0;
return function(e){ // e is for Element here - this is function that is returned unexecuted
var s = e.style;
if(b){ // test if bold
s.fontWeight = 'normal'; b = 0;
}
else{
s.fontWeight = 'bold'; b = 1;
}
}
})();
E('b').onclick = function(){
boldToggle(this); // this within Event refers to Element id='b' in this case
}
} // end onload
//]]>
//
哦,如果使用
这将是一个提交按钮,那么请执行以下操作:
<button type='button' id='b'>Bold</button>
粗体
我更喜欢:
<input type='button' id='b' value='Bold' />
更清楚的是,因为许多浏览器无法访问
上的.innerHTML
,因为它实际上是一种输入,所以
标记可能会误导用户。我在这里更正一些代码时了解到了这一点,用户在
中嵌套了其他元素。这在某些浏览器上不起作用,所以我更喜欢
。如果你在大型项目上工作,这可以防止其他人试图在其中抛出不属于你的元素。使用外部JavaScript,因此它会被缓存,以加快加载时间,因为你应该将HTML与JavaScript分离,这是一种最佳做法。但实际上,您的问题是每次调用函数时都在重新定义var-ban
。观察和学习:
//<![CDATA[
// change the pre var on any page using this onload technique
var pre = onload, doc, bod, htm, E, boldToggle; // higher scope in case other onloads need access
onload = function(){ // same as window.onload - note that window is implicit
if(pre)pre(); // execute previous onload
doc = document, bod = doc.body, htm = doc.documentElement;
E = function(id){
return doc.getElementById(id);
}
boldToggle = (function(){ // Anonymous function executes and scopes off var b before returning an unexecuted function
var b = 0;
return function(e){ // e is for Element here - this is function that is returned unexecuted
var s = e.style;
if(b){ // test if bold
s.fontWeight = 'normal'; b = 0;
}
else{
s.fontWeight = 'bold'; b = 1;
}
}
})();
E('b').onclick = function(){
boldToggle(this); // this within Event refers to Element id='b' in this case
}
} // end onload
//]]>
//
哦,如果使用
这将是一个提交按钮,那么请执行以下操作:
<button type='button' id='b'>Bold</button>
粗体
我更喜欢:
<input type='button' id='b' value='Bold' />
更清楚的是,因为许多浏览器无法访问
上的.innerHTML
,因为它实际上是一种输入,所以
标记可能会误导用户。我在这里更正一些代码时了解到了这一点,用户在
中嵌套了其他元素。这在某些浏览器上不起作用,所以我更喜欢
。如果你曾经在大型项目上工作过,这可以防止其他人试图将不属于你的元素扔进去。
function Bold()
{
var ban=document.getElementById(“texto”).style.fontWeight;
如果(ban=‘正常’)
{
document.getElementById(“texto”).style.fontwweight='bold';
}
其他的
{
document.getElementById(“texto”).style.fontWeight='normal';
}
}
粗体
你好
function Bold()
{
var ban=document.getElementById(“texto”).style.fontWeight;
如果(ban=‘正常’)
{
document.getElementById(“texto”).style.fontwweight='bold';
}
其他的
{
document.getElementById(“texto”).style.fontWeight='normal';
}
}
粗体
Hi
使用类来管理CSS样式
function bold(){document.getElementById('b').classList.toggle('bold');}
.bold{font-weight:bold;}
Bold
使用类来管理CSS样式
function bold(){document.getElementById('b').classList.toggle('bold');}
.bold{font-weight:bold;}
Bold
在调试器中单步执行代码时会看到什么?在调试器中单步执行代码时会看到什么?元素。无法在所有浏览器上访问style
属性。糟糕的技术。@PHPglue查看页面。所有主要浏览器都支持它们还声称您也可以像那样获得元素.style.width
,但这在某些浏览器上不起作用。请参见获取样式信息。学校可以提供帮助,但并不总是完全可靠的。尽管我承认这项技术可能适用于许多新浏览器,但我仍然不推荐它。@PHPglue撇开这是否是最佳方法的问题不谈,在哪些浏览器上是element.style
properties不可读取?element.style
属性不可在所有浏览器上读取。糟糕的技术。@PHPglue查看页面。所有主要浏览器都支持它们还声称您也可以像那样获得元素.style.width
,但这在某些浏览器上不起作用。请参见获取样式信息。学校可以提供帮助,但并不总是完全可靠的。尽管我承认这项技术可能适用于许多新浏览器,但我仍然不推荐它。@PHPglue撇开这是否是最佳方法的问题不谈,哪些浏览器上的element.style
属性不可读取?您应该将HTML与JavaScript分开,将CSS与HTML和JavaScript分开怎么样?您应该将HTML与JavaScript分开,将CSS与HTML和JavaScript分开怎么样?