jquery和创建样式,如何检查样式是否已经存在
大家好,我环顾四周,但似乎找不到我正在寻找的答案。
我有一个搜索页面,它进行 ajax 调用,返回 json 数据,然后根据返回的数据创建一个样式并将其附加到 DOM 部分。 样式的创建工作正常,但如果进行新的搜索,则样式会重复,因此我的 $(#element).slidetoggle() 函数会重复,并且 Slidetoggle 会立即打开和关闭。
您可以在此处查看该页面: http://www.reelfilmlocations.co.uk/ NEW%20Search/fullsearch_jq_keys.php
(用作测试数据hounslow、北伦敦和类别:墓地) 如果进行两次相同的搜索,结果的样式会重复,并且滑动切换行为也会重复。)
我的问题是如何检查样式是否已经存在。
创建样式的脚本如下:
function makeCSS(id){
var myUrl = 'getBoroughInfo.php';
$.ajax({
url: myUrl,
type: 'POST',
data: 'myID='+id,
dataType: 'json',
error: function(xhr, statusText, errorThrown){
// Work out what the error was and display the appropriate message
},
success: function(myData){
var items = myData.boroughs[0];
//console.log('borough: '+myData.boroughs);
$("<style type='text/css'> #link_"+items.Borough_ID+"{ color:"+items.color+"; font-weight:bold; float:left; margin-left: 10px; cursor: pointer; padding: 1px; border: 1px solid "+items.color+";} </style>").appendTo("head");
$("<style type='text/css'> #borough_"+items.Borough_ID+"{ color:"+items.color+"; font-weight:bold; } </style>").appendTo("head");
$("<style type='text/css'> #searchHead_"+items.Borough_ID+"{ color:#000000; font-weight:bold; background-color:"+items.color+"; opacity: 1.0; cursor: pointer;} </style>").appendTo("head");
//apply opacity
//$('#borough_'+items.Borough_ID).css({});
$('#borough_links').append('<div id="link_'+items.Borough_ID+'">'+items.Borough_Name+'</div>');
$('#results').append('<div id="searchHead_'+items.Borough_ID+'">'+items.Borough_Name+'</div>');
$('#results').append('<div id="borough_'+items.Borough_ID+'"></div>');
$('#link_'+items.Borough_ID).live('click', function(){
$('#borough_'+items.Borough_ID).slideToggle('slow', function() {
// Animation complete.
});
});
$('#searchHead_'+items.Borough_ID).live('click', function(){
$('#borough_'+items.Borough_ID).slideToggle('slow', function() {
// Animation complete.
});
});
}
});
};
这是创建我的样式之一的代码行:
$("<style type='text/css'> #link_"+items.Borough_ID+"{ color:"+items.color+"; font-weight:bold; float:left; margin-left: 10px; cursor: pointer; padding: 1px; border: 1px solid "+items.color+";} </style>").appendTo("head");
那么我如何检查文档中是否已经存在该样式??? 我认为一个简单的 if 语句就足以满足我的需求。
Hi people i have looked around but cant seem to find the answer i'm looking for.
i have a search page which makes ajax calls, returns json data then creates a style based on the returned data and appends it to the section of the DOM.
The creation of the styles is working ok, but if a new search is made the style is duplicated and so my $(#element).slidetoggle() function gets duplicated and the slidetoggle opens and closes immedietly.
ypu can see the page here: http://www.reelfilmlocations.co.uk/NEW%20Search/fullsearch_jq_keys.php
(use as test data hounslow, north london and from categories: cemeteries)
if the same search is made twice the styles for the results gets duplicated, and the slide toggle behaviour is duplicated.)
my question is how do i check if the style is already in existence.
the script that creates the style is as follows:
function makeCSS(id){
var myUrl = 'getBoroughInfo.php';
$.ajax({
url: myUrl,
type: 'POST',
data: 'myID='+id,
dataType: 'json',
error: function(xhr, statusText, errorThrown){
// Work out what the error was and display the appropriate message
},
success: function(myData){
var items = myData.boroughs[0];
//console.log('borough: '+myData.boroughs);
$("<style type='text/css'> #link_"+items.Borough_ID+"{ color:"+items.color+"; font-weight:bold; float:left; margin-left: 10px; cursor: pointer; padding: 1px; border: 1px solid "+items.color+";} </style>").appendTo("head");
$("<style type='text/css'> #borough_"+items.Borough_ID+"{ color:"+items.color+"; font-weight:bold; } </style>").appendTo("head");
$("<style type='text/css'> #searchHead_"+items.Borough_ID+"{ color:#000000; font-weight:bold; background-color:"+items.color+"; opacity: 1.0; cursor: pointer;} </style>").appendTo("head");
//apply opacity
//$('#borough_'+items.Borough_ID).css({});
$('#borough_links').append('<div id="link_'+items.Borough_ID+'">'+items.Borough_Name+'</div>');
$('#results').append('<div id="searchHead_'+items.Borough_ID+'">'+items.Borough_Name+'</div>');
$('#results').append('<div id="borough_'+items.Borough_ID+'"></div>');
$('#link_'+items.Borough_ID).live('click', function(){
$('#borough_'+items.Borough_ID).slideToggle('slow', function() {
// Animation complete.
});
});
$('#searchHead_'+items.Borough_ID).live('click', function(){
$('#borough_'+items.Borough_ID).slideToggle('slow', function() {
// Animation complete.
});
});
}
});
};
this is the line of code that creates one of my styles:
$("<style type='text/css'> #link_"+items.Borough_ID+"{ color:"+items.color+"; font-weight:bold; float:left; margin-left: 10px; cursor: pointer; padding: 1px; border: 1px solid "+items.color+";} </style>").appendTo("head");
so how do i check if this exists in the document already????
a simple if statement would suffice for my needs i think.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 :contains 选择器。
例如:
You can use :contains selector.
e.g: