У меня есть код, который я нашел в Интернете, что делает оба div на моем сайте равными. Однако этот код работает только после обновления страницы, и я понятия не имею, что может вызвать это. Любая помощь будет оценена!
// EQUAL HEIGHTS
$.fn.equalHeights = function(px) {
$(this).each(function(){
var currentTallest = 0;
$(this).children().each(function(i){
if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
});
if (!px && Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
// for ie6, set height since min-height isn't supported
if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
$(this).children().css({'min-height': currentTallest});
});
return this;
};
// just in case you need it...
$.fn.equalWidths = function(px) {
$(this).each(function(){
var currentWidest = 0;
$(this).children().each(function(i){
if($(this).width() > currentWidest) { currentWidest = $(this).width(); }
});
if(!px && Number.prototype.pxToEm) currentWidest = currentWidest.pxToEm(); //use ems unless px is specified
// for ie6, set width since min-width isn't supported
if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'width': currentWidest}); }
$(this).children().css({'min-width': currentWidest});
});
return this;
};
// CALL EQUAL HEIGHTS
$(function(){ $('#equalize').equalHeights(); });
Такое поведение происходит из-за плохого написания плагина. Я изменил его для вас, и теперь он должен работать.
Сценарий плагина:
// EQUAL HEIGHTS
$.fn.equalHeights = function(px) {
var currentTallest = 0;
$(this).each(function(){
$(this).siblings().each(function(i){
if ($(this).height() > currentTallest) {
currentTallest = $(this).height();
}
});
});
if (!px && Number.prototype.pxToEm) {
currentTallest = currentTallest.pxToEm();
}
$(this).siblings().css({'height': currentTallest, 'min-height': currentTallest});
return this;
};
Вы можете изменить плагин equalWidths
для работы в качестве этого плагина.
У меня такая же проблема. Я пытаюсь загрузить canvas как изображение, но мой код работает только во второй раз. Первый раз это не работает. Я буду осторожен, если вы мне поможете.
$(document).ready(function () {
$('#save').on('click', function () {
html2canvas($('#canvas'), {
onrendered: function (canvas) {
var saveAs = function (uri, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
document.body.appendChild(link); // Firefox requires the link to be in the body
link.download = filename;
link.href = uri;
link.click();
document.body.removeChild(link); // remove the link when done
} else {
location.replace(uri);
}
};
var img = canvas.toDataURL("jpg", 0.90),
uri = img.replace(/^data:image\/[^;]/, 'data:application/octet-stream');
scale: 6;
dpi: 192;
width: 2482;
height: 4508;
letterRendering: true;
saveAs(uri, 'Imza.jpg');
}
});
});
});
поместите весь свой код в функцию jquery ready. внутри готовый код функции выполняется, когда DOM готов
$(document).ready(function(){
//your code
});
$(function() { ... })
совпадает с $(document).ready(function() { ... })
, и он уже поместил свой код в $(function() { ... });
, Упоминается в API .