[英]Get previous Span in Html
Hi I'm trying to get the previous span in my html that was used as label, I've tried jquery .prev([selector]), but the selector doesn't work, it return the parent span of the input field I'm getting.嗨,我正在尝试获取用作标签的 html 中的前一个跨度,我尝试过 jquery .prev([selector]),但选择器不起作用,它返回输入字段的父跨度我得到。
here is my HTML and script:这是我的 HTML 和脚本:
<form>
<span class="a-text-bold">
Add product detail
</span>
<div class="a-section a-spacing-base">
<div class="a-section a-spacing-mini">
<span class="a-text-bold">
Brand name
</span>
</div>
<span class="a-declarative" data-action="form-item-action">
<span class="a-declarative">
<input type="text" maxlength="50" value="Bubble Goth Babes" id="data-draft-brand-name" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal gear-filter-control-characters-textbox lock-needs-hidden">
</span>
</span>
</div>
<div class="a-section a-spacing-base">
<div class="a-section a-spacing-mini">
<span class="a-text-bold">
Title of product
</span>
</div>
<span class="a-declarative" data-action="form-item-action">
<span class="a-declarative">
<input type="text" maxlength="50" value="Cute Zombkitty Zombie Kitten Neon Funny Brain Cat" id="data-draft-name-en-us" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal gear-filter-control-characters-textbox lock-needs-hidden">
</span>
</span>
</div>
<button onclick="getLabelAndValue()">Get</button>
</form>
<script type="text/javascript">
function getLabelAndValue(){
var focused = document.activeElement;
var form = document.getElementsByName(focused.form.name);
for (var i = 0; i < form.length; i++) {
if(form[i].tagName.toLowerCase() == "input" && form[i].getAttribute('type').toLowerCase() == "text"){
console.log($("#" + form[i].id).prev("span .a-text-bold").text()); //Doesn't work
console.log(form[i].value); //Already working, means I'm pointing to right element
}
}
}
</script>
What I'm trying to see on the console.log are:我想在 console.log 上看到的是:
//Brand Name
//Bubble Goth Babes
//Title of Product
//Cute Zombkitty Zombie Kitten Neon Funny Brain Cat
note : I'm already able to get the form elements value ( form[i].value
), just the span before it with className a-text-bold
is what I need to get.注意:我已经能够获得表单元素值( form[i].value
),只是它之前的跨度与 className a-text-bold
是我需要获得的。
I would stick with using jQuery for everything, since it's so easy to use.我会坚持使用 jQuery,因为它非常易于使用。 Here's essentially the same code, only getting the label a different way.这基本上是相同的代码,只是以不同的方式获取标签。 Note that you can't use prev()
, since the span
is not a sibling of the input
.请注意,您不能使用prev()
,因为span
不是input
的同级。 You have to travel up the DOM to a common ancestor, then back down the the span
.您必须将 DOM 向上移动到一个共同的祖先,然后向下返回span
。
function getLabelAndValue(e) { e.preventDefault(); var form = e.target.form; var inputs = $('input[type="text"]', form); inputs.each(function (input) { // get the closest common ancestor, then find the "a-text-bold" span. var label = $(this).closest(".a-section").find(".a-text-bold").text().trim(); console.log(label); console.log(this.value); //Already working, means I'm pointing to right element }); } window.onload = function() { document.getElementById("get").addEventListener("click", getLabelAndValue); };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <span class="a-text-bold"> Add product detail </span> <div class="a-section a-spacing-base"> <div class="a-section a-spacing-mini"> <span class="a-text-bold"> Brand name </span> </div> <span class="a-declarative" data-action="form-item-action"> <span class="a-declarative"> <input type="text" maxlength="50" value="Bubble Goth Babes" id="data-draft-brand-name" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal gear-filter-control-characters-textbox lock-needs-hidden"> </span> </span> </div> <div class="a-section a-spacing-base"> <div class="a-section a-spacing-mini"> <span class="a-text-bold"> Title of product </span> </div> <span class="a-declarative" data-action="form-item-action"> <span class="a-declarative"> <input type="text" maxlength="50" value="Cute Zombkitty Zombie Kitten Neon Funny Brain Cat" id="data-draft-name-en-us" autocomplete="off" name="data[draft][brand_name]" class="a-input-text a-form-normal gear-filter-control-characters-textbox lock-needs-hidden"> </span> </span> </div> <button id="get">Get</button> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.