Javascript 获取具有特定id和innerhtml的最接近的div

Javascript 获取具有特定id和innerhtml的最接近的div,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有5个完全相同的div,在每个div中我有一个select选项输入。。所有的div都有相同的id,所以我想获得select的最近div,然后在其中执行innerHTML 所以我想要这样的东西: $(this).closest('div[maybeidthere?]')。innerhtml='whole content here' 提前谢谢 编辑:忽略相同的id,假设所有id都具有相同的类 <div> <select id="faculty" onch

我有5个完全相同的div,在每个div中我有一个select选项输入。。所有的div都有相同的id,所以我想获得select的最近div,然后在其中执行innerHTML

所以我想要这样的东西:

$(this).closest('div[maybeidthere?]')。innerhtml='whole content here'

提前谢谢

编辑:忽略相同的id,假设所有id都具有相同的类

    <div> 
        <select id="faculty" onchange="inchange(this)">
                    <option value="fos">Faculty of Science</option>
                    <option value="fot">Faculty of Information Technology</option>
                   <option value="foe">Faculty of Engineering</option>
                    <option value="other">Other</option>
        </select>
         <div class="degreediv"></div>

        </div>
                <script>
function inchange(input){


    if($(input).val()=="fot"){
     $(this).closest('div[.degreediv]').innerhtml ='FOT CONTENT';

    }
    else if($(input).val()=="fos"){
        $(this).closest('div[.degreediv]').innerhtml ='FOS CONTENT';
    }
    else if($(input).val()=="foe"){
        $(this).closest('div[.degreediv]').innerhtml ='FOE CONTENT';
    }
    else if($(input).val()=="other"){
        $(this).closest('div[.degreediv]').innerhtml ='other CONTENT';
    }
};
</script>

理学院
信息技术学院
工程学院
另外
功能改变(输入){
如果($(输入).val()=“fot”){
$(this).closest('div[.degreediv]')。innerhtml='FOT CONTENT';
}
else if($(输入).val()=“fos”){
$(this).closest('div[.degreediv]')。innerhtml='FOS CONTENT';
}
else if($(输入).val()=“foe”){
$(this).closest('div[.degreediv]')。innerhtml='FOE CONTENT';
}
else if($(输入).val()=“其他”){
$(this).closest('div[.degreediv]')。innerhtml='other CONTENT';
}
};

由于select在div中,因此可以使用.Parent()函数

$(this.parent().innerhtml

<select>
    <option value="value1">1</option>
    <option value="value2">2</option>
    <option value="value3">3</option>
</select>
<div>this is div 1</div>
<div>this is div 2</div>
...
<div>this is div n</div>

请注意,
jQuery
选择器的工作原理与
CSS
选择器类似

请参阅:

CSS选择器参考

jQuery选择器参考


离什么最近<代码>:首先是类型
?因为id应该是唯一的,所以开始修复它,然后发布您的代码,以便我们可以在建议之前查看任何内容
id
必须是不同的,它必须唯一地标识页面上的元素
class
可以多次使用。好吧,让我们忽略id,假设我没有div的id。。与我的选择选项最接近的div请参见更新我的答案。不能在jQuery包装器上使用
.innerhtml
$textOfDiv = $('select + div').val(); // selects text inside first DIV element
$htmlOfDiv = $('select + div').html(); // selects html inside first DIV element