如何从我的HTML对象和jQuery中的src图像中获取类名? [英] How do I get class name from my HTML object and src of image in jQuery?
问题描述
我想从JavaScript(jQuery)中获取HTML对象的类名,并将其写入变量。我试图在jQuery的文档中找到它,但我并没有成功。接下来,我想获得图像的来源(图像的路径)或段落的文本(p)。
如何从我的名字中获取类名HTML对象?如何从段落中获取图像源或文本?
请帮帮我。
这里,我插入元素:如果Textbox为空我插入图像,否则我插入段落。
Hi,
I want to get a class name from my HTML object in JavaScript (jQuery) and write it into a variable. I was trying to find it in the documentation for jQuery, but I wasn''t successful. Next, I want to get source of an image (path to the image) or a text of a paragraph (p).
How can I get class name from my HTML object? And how can I get image source or text from paragraph?
Please, help me.
Here, I am inserting the elements: if Textbox is empty I insert image, else I insert paragraph.
if ($("#Textbox1").val().toString() == "") {
//if user has not written any text, inserts image to screen.
$("#platno").append("<div class=\"draggable\" id=\"" + ImgID + "\" style=\"max-height:100px; max-width:100px;\"><img class=\"img\" src=\"" + path.toString() + "\"></div>");
}
else {
//else inserts Text
$("#platno").append("<div class=\"draggable\" id=\"" + ImgID + "\" style=\"max-height:100px; max-width:100px;\"><p class=\"text\">" + $("#Textbox1").val() + "</p></div>");
}
我试图获得一个类和一个图像源,但这不起作用:
I tried to get a class and a source of an image, but this doen''t work:
elem[2] = $(selector.toString()).children().first().css("class"); //third attribute is class (text or image)
if ($(selector.toString()).children().first().css("class") == "img") {
elem[3] = $(selector.toString()).children().first().css("src"); //fourth is image source
}
else {
elem[3] = $(selector.toString()).children().first().text; //or fourth is text of an element
}
感谢您的回复!
Pepin z Hane
Thanks for replies!
Pepin z Hane
推荐答案
( #Textbox1)。val()。toString()== < span class =code-string> ){
// 如果用户没有写任何文本,则将图像插入屏幕。
("#Textbox1").val().toString() == "") { //if user has not written any text, inserts image to screen.
( #platno)。append( < div class = \draggable\id = \ + ImgID + \style = \max-height:100px; max-width:100px; \>< img class = \img \src = \ +路径的ToString( )+ \>< / div>);
}
其他 {
// else插入文本
("#platno").append("<div class=\"draggable\" id=\"" + ImgID + "\" style=\"max-height:100px; max-width:100px;\"><img class=\"img\" src=\"" + path.toString() + "\"></div>"); } else { //else inserts Text
( # platno)。append( < div class = \draggable\id = \\ \\ + ImgID + \style = \max-height:100px; max -width:100px; \>< p class = \text \> +
("#platno").append("<div class=\"draggable\" id=\"" + ImgID + "\" style=\"max-height:100px; max-width:100px;\"><p class=\"text\">" +
这篇关于如何从我的HTML对象和jQuery中的src图像中获取类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!