简体   繁体   English

按钮颜色不变

[英]Button colour not changing

Problem- Button colour is not changing, I used jquery click function 问题-按钮的颜色没有改变,我使用了jquery点击功能

<button class="red"  id="MyQuotes" href="#" title="">@Aceo.Crm.App.Resources.Shared.Order.Link_MyQuote</button>

For this button i used jquery as 对于这个按钮,我使用jQuery作为

$(document).ready(function(){
    $("#MyQuotes").click(function(){
    $(this).css({background:"red"});
    });
});

But it was not successfull, I tried to do this in this way too- 但这并不成功,我也尝试过这种方式-

<button class="red" onclick="D()" id="MyQuotes" href="#" title="">@Aceo.Crm.App.Resources.Shared.Order.Link_MyQuote</button>

I made javascript function here as- 我在这里做了javascript函数-

<script language="javascript">
function D()
{
document.body.style.backgroundColor="red";
}
</script>

And yes this time i was again failed. 是的,这次我再次失败了。 Can you please suggest me some codes? 你能给我建议一些代码吗?

Using jQuery: http://jsfiddle.net/DrWjq/ 使用jQuery: http : //jsfiddle.net/DrWjq/

$(document).ready(function(){
   $("#MyQuotes").click(function(){
    $(this).css({background:"red"});
   });
});

Pure JS: http://jsfiddle.net/wx9tw/ 纯JS: http//jsfiddle.net/wx9tw/

function D(id)
{
 id.style.backgroundColor="red";
}

<button class="red" onclick="D(this)" id="MyQuotes" href="#" title="">@Aceo.Crm.App.Resources.Shared.Order.Link_MyQuote</button>

your selector's id is "MyQuotes" and not "MyOrders" 您选择器的ID是“ MyQuotes”,而不是“ MyOrders”

try this 尝试这个

$("#MyQuotes").click(function(){
  $(this).css({background:"red"});
});

OR 要么

function D()
{
  $('#MyQuotes').css({background:"red"});
}

You must refer the button (SO #MyQuotes not #MyOrders) in JS: 您必须在JS中引用按钮(因此,不是#MyQuotes,而是#MyQuotes):

$(document).ready(function(){
    $("#MyQuotes").click(function(){
    $(this).css({background:"red"});
    });
});

You can use $(this).css("background-color","red"); 您可以使用$(this).css("background-color","red"); (and target the correct element id) (并指定正确的元素ID)

fiddle 小提琴

JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> - JavaScript for changing a <div> colour from a button press

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重新加载时按钮颜色不会改变 - Button colour is not changing on reload 使用 CSS 更改按钮颜色 - Changing button colour using CSS 更改按钮的背景颜色(开/关) - Changing the background colour of a button (On/Off) 使用按钮java脚本更改某些内容的颜色 - Changing the colour of something using button java script Java语言边框颜色在单选按钮单击时未更改 - Javascript border colour not changing on radio button click 使用 React 中的按钮单击更改 div 颜色样式 - Changing div colour styling with button click in React JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> - JavaScript for changing a <div> colour from a button press React/Redux 使用状态更改嵌套数组中按钮的背景颜色 - React/Redux Changing background colour of button in nested array using state 单击按钮时更改 Reactjs 中特定元素的颜色 - Changing the colour of specific elements in Reactjs when clicking a button 使用上一个/下一个按钮时更改活动标签的颜色 - Changing active tab colour when using a previous/next button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM