gpt4 book ai didi

使用jquery addClass()方法的javascript函数不添加类

转载 作者:行者123 更新时间:2023-11-30 07:26:18 26 4
gpt4 key购买 nike

脚本可以正常工作,可以正确创建所有内容等等,但是 jquery addClass 方法没有添加类。我不熟悉在 javascript 函数中使用 jquery 方法,感谢任何帮助,包括在不使用 jquery 的情况下添加适当类的替代方法。

function thumbnail(Img, Element, Modal, ModalTitle) {
"use strict";
/*jslint browser:true*/
/*global $, jQuery*/
//this function will append an anchor tag with the appropriate functionality, accepting inputs on the image filename, attaching element, and modal
//Img = full filename in format of foo.png
//Element = the ID of the element within which the thumbnail anchor will be placed
//Modal = the ID of the modal
//ModalTitle = Text used for title of the modal and title of the caption
var image, element, modal, loc, output, iUrl, modal_loc, modal_output, mtitle;
image = Img;
element = Element;
modal = Modal;
mtitle = ModalTitle;
iUrl = "/design-library/images/thumbs/" + image;
output = "<a href='#' data-reveal-id='" + modal + "'>";
output += "<img class='sample_img' src='" + iUrl + "' alt='" + mtitle + "' />";
output += "</a>";
output += "<p class='caption'>" + mtitle + "</p>";
modal_output = "<h1>" + mtitle + "</h1>";
modal_output += "<img src='" + iUrl + "' alt='" + image + "' /><a class='close-reveal-modal'>&#215;</a>";
//create the modal container
$(modal).addClass('reveal-modal');
modal_loc = document.getElementById(modal);
modal_loc.innerHTML = modal_output;
//the end of the script gets the element and adds the anchor tag that exists in output
$(element).addClass('samples');
loc = document.getElementById(element);
loc.innerHTML = output;
}

最佳答案

由于 modalelement 是 ID,您应该更正您的选择器以将它们用作一个:

$('#' + modal).addClass('reveal-modal');
$('#' + element).addClass('samples');

旁注。使用 jQuery 找到 DOM 元素后,无需使用 getElementById 执行第二次搜索:

var modal_loc = $('#' + modal);
modal_loc.addClass('reveal-modal');
modal_loc.html(modal_output);

关于使用jquery addClass()方法的javascript函数不添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13861613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com