简体   繁体   English

为什么我的jQuery .css函数不起作用?

[英]Why is my jQuery .css function not working?

I'm building some kind of add/remove taglist that is connected with mySQL. 我正在构建某种与mySQL连接的添加/删除标记列表。 I've managed to get the tags from the database to display with a ajax call, but i can't do any kind of operation to them. 我已经设法从数据库中获取标签以显示ajax调用,但我无法对它们进行任何操作。 Not even a common style. 甚至没有共同的风格。 When i check with Firebug all the html seems to be in place so i can't figure out what's wrong. 当我用Firebug检查时,所有的html似乎都已到位,所以我无法弄清楚出了什么问题。 Here is my code: 这是我的代码:

jQuery: jQuery的:

 $(document).ready(function() {

   $("#ontvangenjson").css("border","3px solid red");


   $.getJSON("jason2.php", function(data) {

      $.each(data, function(){

        var merkTag = " <a class=\"deletemerk\" href="+"http://localhost/website/remove_merk.php?id="+this.pkFavorietemerken+">" + this.merken + "</a>";

         $("#ontvangenjson").append(merkTag);

               });

           });

       });

PHP: jason2.php PHP: jason2.php

 $merken_lijst = "SELECT favorietemerken.pkFavorietemerken, favorietemerken.merken FROM favorietemerken JOIN bedrijven ON bedrijven.pkBedrijvenID=favorietemerken.fkBedrijvenID WHERE favorietemerken.fkBedrijvenID=$neem_id";


 $rows = array();
 $sth = mysql_query($merken_lijst);
 while($r = mysql_fetch_assoc($sth)) {
 $rows[] = $r;
}
print json_encode($rows);

RECEIVED JSON: 收到的JSON:

 [{"pkFavorietemerken":"71","merken":"Nike"},{"pkFavorietemerken":"70","merken":"Le Coq Sportif"},{"pkFavorietemerken":"69","merken":"Converse"},{"pkFavorietemerken":"68","merken":"Champion"},{"pkFavorietemerken":"67","merken":"Adidas"}] 

HTML: HTML:

<body>


  <h1><label for="brands-form-brand">Get JSON data</label> <input type="button" id="knop" value="get JSON" /></h1>

  <hr />

  <p class="section-title"><strong>JSON Data received</strong></p>


  <div id="ontvangenjson">  </div> 

</body>

ANSWER 回答

After alot, alot,alot of research I've finaly solved this problem. 经过很多很多研究,我最终解决了这个问题。 The code wasn't really wrong but a piece of it was misplaced. 代码并没有真正错误,但其中一部分是错误的。 The get.JSON is asynchronous meaning if you want to make any changes using the jQuery .css function you will need to do that inside the callback for the getJSON. get.JSON是异步的意思,如果你想使用jQuery .css函数进行任何更改,你需要在getJSON的回调中做到这一点。

  $.getJSON("jason2.php", function(data) {
   var merkTag = "";
  $.each(data, function(){
  merkTag += " <a class=\"deletemerk\" href="+"http://localhost/website/remove_merk.php?id="+this.pkFavorietemerken+">" + this.merken + "</a>";
  });

  $("#ontvangenjson").append(merkTag);

  // NEW CODE
  $(".deletemerk").css("border","3px solid red");
  }); 

Shorthand properties (such as border ) are not supported by jQuery.css() . jQuery.css()不支持速记属性(例如border jQuery.css()

http://api.jquery.com/css/ http://api.jquery.com/css/

如果要使用border的所有三个属性,请尝试单独定义它们,它可能会解决您的问题。

I'm not sure this will solve your problem, but I would start by trying to clean up the way you're making that link. 我不确定这会解决你的问题,但我会先尝试清理你建立链接的方式。 You may have missed one or two quotes. 您可能错过了一两个引号。 Instead of the confusion of escaping or failing to escape double quotes, just surround your string with single quotes: 而不是逃避或无法逃避双引号的混淆,只需用单引号包围你的字符串:

var merkTag = '<a class="deletemerk" href="http://localhost/website/remove_merk.php?id='
              + this.pkFavorietemerken 
              + '">' 
              + this.merken 
              + '</a>';

This leaves you free to use double quotes when you're building an HTML string. 这使您可以在构建HTML字符串时自由使用双引号。 I've used multiple lines here just for clarity's sake. 为了清楚起见,我在这里使用了多行。 Clarity over brevity. 清晰而简洁。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM